﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Drawing.Printing;

using CodeDynamiksEngine;
using MESCodeFile;
using LiveNotificationSystem;

namespace mes_tublay
{
    public partial class frmApplicationBusinessPermit : Form
    {
        CodeProcessor proc = new CodeProcessor(ThreadingMode.Normal);
        CodeScript script;
        DataCache dat = new DataCache("MyData");
        string businessReferenceNumber = "";

        //update data structure for data concurrency
        updateInfo businessOwner = new updateInfo();
        updateInfo businessProfile = new updateInfo();
        updateInfo businessInformation = new updateInfo();
        updateInfo referenceNumberInfo = new updateInfo();
        updateInfo applicationNumberInfo = new updateInfo();
        updateInfo advanceBusinessOwner = new updateInfo();
        updateInfo advanceReferenceInfo = new updateInfo();

        //variables used for application number
        bool isExistingDate = false;
        bool isExisitngOwnerProfile = false;
        bool isOnError = false;
        string businessType = "SINGLE";
        bool hasImage = false;
        string fImageName = "";

        //variable used for reference Number
        int currentRefNum = 0;
        int advanceRefNum = 0;

        //variable used for checking what action did the user choose
        string myAction = "";
        string myReferenceNumber = "";
        string myBusinessReferenceNumber = "";
        string myPreviousApplicationNumber = "";
        string myStatus="";

        //constructor for Additional Business Permit and Renewal 
        public frmApplicationBusinessPermit(myParam info)
        {
            InitializeComponent();
            InitializeFrameSliderForm();
            
            myAction = info.action;
            myReferenceNumber = info.myReferenceNumber;
            myBusinessReferenceNumber = info.myBusinessReferenceNumber;
            myPreviousApplicationNumber = info.myApplicationNumber;
            myStatus = info.status;

            lblMsg.Text = info.msg.ToUpper();
        }
        //contructor for New Business Permit
        public frmApplicationBusinessPermit()
        {
            InitializeComponent();
            InitializeFrameSliderForm();
        }

        private void InitializeForm()
        {
            single.Click += new EventHandler(single_Click);
            corporation.Click += new EventHandler(corporation_Click);
            partnership.Click += new EventHandler(partnership_Click);
            chkYes.Click += new EventHandler(chkYes_Click);
            chkSame.Click += new EventHandler(chkSame_Click);
            btnCancel.Click += new EventHandler(btnCancel_Click);
            txtDateOfApplication.ValueChanged += new EventHandler(txtDateOfApplication_ValueChanged);
            btnSave.Click += new EventHandler(btnSave_Click);
            btnCaptureImage.Click += new EventHandler(btnCaptureImage_Click);
            btnRemoveImage.Click += new EventHandler(btnRemoveImage_Click);
            btnPrint.Click += new EventHandler(btnPrint_Click);
            btnMayorApproval.Click += new EventHandler(btnMayorApproval_Click);
            btnTreasury.Click += new EventHandler(btnTreasury_Click);
            
            //error trapping : check if the last name, first name and middle name is existing or not
            txtLastName.LostFocus += new EventHandler(txtLastName_LostFocus);
            txtLastName.Leave += new EventHandler(txtLastName_Leave);
            txtFirstName.LostFocus += new EventHandler(txtFirstName_LostFocus);
            txtFirstName.Leave += new EventHandler(txtFirstName_Leave);
            txtMiddleName.LostFocus += new EventHandler(txtMiddleName_LostFocus);
            txtMiddleName.Leave += new EventHandler(txtMiddleName_Leave);
            txtCompanyName.LostFocus += new EventHandler(txtCompanyName_LostFocus);
            txtCompanyName.Leave += new EventHandler(txtCompanyName_Leave);
            txtBusinessName.LostFocus +=new EventHandler(txtBusinessName_LostFocus);
            txtBusinessName.Leave += new EventHandler(txtBusinessName_Leave);

            //code processor
            proc.ProcessCompleted += new EventHandler(proc_ProcessCompleted);
            this.FormClosing += new FormClosingEventHandler(frmApplicationBusinessPermit_FormClosing);

            //initialize controls
            btnSave.Enabled = false;
            single.Checked = true;

            //loading method
            switch (myAction)
            {
                
                
                case "VIEW":
                    if (myStatus == "APPROVED") { btnMayorApproval.Enabled = false;  btnTreasury.Enabled = false; }
                    loadInformation();
                    break;
                case "TRANSFER LOCATION":
                case "TRANSFER OWNERSHIP":
                case "RENEWAL":
                default:
                    btnTreasury.Enabled = true;
                    btnMayorApproval.Enabled = false;
                    initializeGrid(); break;
            }
        }
        
        //leave
        void txtCompanyName_Leave(object sender, EventArgs e)
        {
            if (txtCompanyName.Text != "")
                checkAndGetBusinessOwnerProfile();   
        }
        void txtMiddleName_Leave(object sender, EventArgs e)
        {
            if (txtMiddleName.Text != "")
                checkAndGetBusinessOwnerProfile();   
        }
        void txtFirstName_Leave(object sender, EventArgs e)
        {
            if (txtFirstName.Text != "")
                checkAndGetBusinessOwnerProfile();   
        }
        void txtLastName_Leave(object sender, EventArgs e)
        {
            if (txtLastName.Text != "")
                checkAndGetBusinessOwnerProfile();   
        }
        void txtBusinessName_Leave(object sender, EventArgs e)
        {

            if (txtBusinessName.Text != "")
            {
                checkBusinessNameTradeName();
            }
        }

        //lostfocus
        void txtCompanyName_LostFocus(object sender, EventArgs e)
        {
            if (txtCompanyName.Text != "")
                checkAndGetBusinessOwnerProfile();
        }
        void txtMiddleName_LostFocus(object sender, EventArgs e)
        {
            if (txtMiddleName.Text != "")
                checkAndGetBusinessOwnerProfile();
        }
        void txtFirstName_LostFocus(object sender, EventArgs e)
        {
            if (txtFirstName.Text != "")
                checkAndGetBusinessOwnerProfile();
        }
        void txtLastName_LostFocus(object sender, EventArgs e)
        {
            if (txtLastName.Text != "")
                checkAndGetBusinessOwnerProfile();
        }
        void txtBusinessName_LostFocus(object sender, EventArgs e)
        {
            if (txtBusinessName.Text != "")
            {
                checkBusinessNameTradeName();
            }
        }
       
        //click
        void partnership_Click(object sender, EventArgs e)
        {
            businessType = "PARTNERSHIP";
            executeClearing(true);
            if (myAction.Equals("RENEWAL")) txtReferenceNumber.Text = advanceRefNum.ToString("0000000");
        }
        void corporation_Click(object sender, EventArgs e)
        {
            businessType = "CORPORATION";
            executeClearing(true);
            if (myAction.Equals("RENEWAL")) txtReferenceNumber.Text = advanceRefNum.ToString("0000000");
        }
        void single_Click(object sender, EventArgs e)
        {
            businessType = "SINGLE";
            executeClearing(false);
            if (myAction.Equals("RENEWAL")) txtReferenceNumber.Text = advanceRefNum.ToString("0000000");
        }
        

        void btnRemoveImage_Click(object sender, EventArgs e)
        {
            //Confirmation for image removal
            if (Confirm.Question("Image Removal", "Remove attached image?") == System.Windows.Forms.DialogResult.Yes)
            {
                //Set the image into blank bitmap
                if (hasImage)
                {
                    //imgUser.Image.Dispose();
                    imgApplicant.Image = mes_tublay.Properties.Resources.DefaultProfile;
                    hasImage = false;
                }
            }
        }
        void btnTreasury_Click(object sender, EventArgs e)
        {
            DialogResult dlg = Confirm.Question("Send For Assessment", "Please double check information before sending for Assessment.\r\nDo you wish to continue?");
            if (dlg == System.Windows.Forms.DialogResult.No) return;

            Common.NOTIFY.Push("TREASURY_DEPT_ASSESSMENT", "New Business Permit Application: " + txtApplicationNumber.Text, new string[] { "" + txtApplicationNumber.Text, txtReferenceNumber.Text, businessReferenceNumber, "BPLO" });
            Common.NOTIFY.Push("FIRE_DEPARTMENT", "New Business Permit Application: " + txtApplicationNumber.Text, new string[] { "" + txtApplicationNumber.Text, txtReferenceNumber.Text, businessReferenceNumber, "BPLO" });
            Common.NOTIFY.Push("MHO", "New Business Permit Application: " + txtApplicationNumber.Text, new string[] { "" + txtApplicationNumber.Text, txtReferenceNumber.Text, businessReferenceNumber, "BPLO" });
            Common.NOTIFY.Push("MPDO", "New Business Permit Application: " + txtApplicationNumber.Text, new string[] { "" + txtApplicationNumber.Text, txtReferenceNumber.Text, businessReferenceNumber, "BPLO" });
            Common.NOTIFY.Push("MEO", "New Business Permit Application: " + txtApplicationNumber.Text, new string[] { "" + txtApplicationNumber.Text, txtReferenceNumber.Text, businessReferenceNumber, "BPLO" });
            Common.NOTIFY.Push("MASSO", "New Business Permit Application: " + txtApplicationNumber.Text, new string[] { "" + txtApplicationNumber.Text, txtReferenceNumber.Text, businessReferenceNumber, "BPLO" });


            Confirm.Info("Push Notification Successful", "Push Notification Successfully sent to All Offices for Approval");
        }
        void btnMayorApproval_Click(object sender, EventArgs e)
        {
            DialogResult dlg = Confirm.Question("Send For Approval", "Please double check information before sending for Approval.\r\nDo you wish to continue?");
            if (dlg == System.Windows.Forms.DialogResult.No) return;
            Common.NOTIFY.Push("MAYOR_OFFICE_APPROVAL", "New Business Permit Application: " + txtApplicationNumber.Text + "- For Approval ", new string[] { txtApplicationNumber.Text, txtReferenceNumber.Text, "BPLO" });

            Confirm.Info("Push Notification Successful", "Business Permit Application is pushed to Mayor for Approval");
        }
        void btnPrint_Click(object sender, EventArgs e)
        {
            PrintOath();
        }
        void btnCaptureImage_Click(object sender, EventArgs e)
        {
            //Instantiate an Image Capture form
            frmImageCapture imgCap = new frmImageCapture();
            //Initialize the form
            imgCap.InitializeForm();
            //Add Post Process Text.
            imgCap.AddPostProcessText(txtReferenceNumber.Text);
            //Show the form
            imgCap.ShowDialog();
            //Check if the user has taken an image, if so, pass it to the picture box.
            if (imgCap.HasTakenImage)
            {
                imgApplicant.Image = imgCap.GetImage();
                imgCap.SaveToFileBuffer(Common.LOCPATH, Const.APPLICATION_EBPLS_IMG);
                hasImage = true;
            }
        }
        void btnSave_Click(object sender, EventArgs e)
        {
            if ((businessType.Equals("SINGLE") && txtFirstName.Text != "" && txtLastName.Text != "" && txtMiddleName.Text != "") || (!businessType.Equals("SINGLE") && txtCompanyName.Text != "") && txtBusinessName.Text != "" && txtKindOfBusiness.Text != "" && txtTradeNameFranchise.Text != "")
            {
                DialogResult dlg1 = Confirm.Question("Print Oath of Undertaking", "Did the applicant signed the oath of undertaking?");
                if (dlg1 == System.Windows.Forms.DialogResult.No) return;

                DialogResult dlg2 = Confirm.Question("Save Information", "Please double check your entries before saving.\r\n Do you wish to continue?");
                if (dlg2 == System.Windows.Forms.DialogResult.No) return;

                switch (myAction)
                {
                    case "TRANSFER OWNERSHIP": commitTransferOwnership(); break;
                    case "TRANSFER LOCATION": commitTransferLocation(); break;
                    case "RENEWAL": commitRenewedBusiness(); break;
                    default: commitNewBPLInformation(); break;
                }

            }
            else
            {
                Confirm.Info("Send Information", "Incomplete Information");
            }
        }
        void btnCancel_Click(object sender, EventArgs e)
        {
            btnClose_Click(null, null);
        }
        void chkSame_Click(object sender, EventArgs e)
        {
            if (chkSame.Checked)
            {
                theSameWithOwnerAddress();
            }
            else
            {
                clearTheSame();
            }
        }
        void chkYes_Click(object sender, EventArgs e)
        {
            if (chkYes.Checked)
                txtSpecify.Enabled = true;
            else
            {
                txtSpecify.Text = "";
                txtSpecify.Enabled = false;
            }
        }
        
        //others
        void frmApplicationBusinessPermit_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (hasImage)
            {
                //imgApplicant.Image.Dispose();
                imgApplicant.Image = mes_tublay.Properties.Resources.DefaultProfile;
                hasImage = false;
            }
        }
        void txtDateOfApplication_ValueChanged(object sender, EventArgs e)
        {
            if(!myAction.Equals("VIEW"))
             generateAppNo();
        }

        //process completed
        void proc_ProcessCompleted(object sender, EventArgs e)
        {
            proc.SafeCall(this, delegate 
            {
                if (proc.State == ProcessorFlag.Error ||
                    proc.State == ProcessorFlag.DataRdyErr ||
                    proc.State == ProcessorFlag.ReadyErr)
                {
                    switch (proc.GetLastProcessName())
                    {
                        default:
                            isOnError = true;
                            MessageBox.Show(proc.GetProcessorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            proc.ClearErrorFlag();
                            initializeGrid();
                            break;
                    }
                    return;
                }
                switch (proc.GetLastProcessName().ToString())
                {
                    case "CheckBusinessName":
                        proc.DataFetch(out dat);
                        dat.SetActivePage(0);
                        if (dat.GetRecordCount(0) > 0)
                        {
                            MessageBox.Show("The Business Name already exists...\n\rPlease use other name...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            txtBusinessName.Text = "";
                            txtBusinessName.Focus();
                        }
                        break;
                    case "INIT_GRID":
                        proc.DataFetch(out dat);
                        int pageNumber = 0;

                        switch (myAction)
                        {
                            case "TRANSFER LOCATION":
                            case "TRANSFER OWNERSHIP":
                            case "RENEWAL":
                                pageNumber = 0;
                                txtReferenceNumber.Text = myReferenceNumber;
                                break;
                            default: pageNumber = 1;
                                dat.SetActivePage(0);
                                currentRefNum = int.Parse(dat.GetValue(0, "fCurNum").ToString());

                                referenceNumberInfo.fID = dat.GetValue(0, "fID").ToString();
                                referenceNumberInfo.fStamp = DateTime.Parse(dat.GetValue(0, "fStamp").ToString());

                                txtReferenceNumber.Text = currentRefNum.ToString("0000000");
                                break;
                        }
                        
                        //use for generation of application number
                        int curApp = 0;
                        string appNo = "";

                        //Set the active page before exporting
                        dat.SetActivePage(pageNumber);
                        if (dat.GetRecordCount(pageNumber) == 1)
                        {
                            isExistingDate = true;
                            applicationNumberInfo.fID = dat.GetValue(0, "fID").ToString();
                            applicationNumberInfo.fStamp = DateTime.Parse(dat.GetValue(0, "fStamp").ToString());

                            curApp = int.Parse(dat.GetValue(0, "fCurNum").ToString());
                            appNo = txtDateOfApplication.Value.ToString("yyyy-MM-dd") + "-" + curApp.ToString("0000");
                        }
                        else
                        {
                            isExistingDate = false;
                            appNo = txtDateOfApplication.Value.ToString("yyyy-MM-dd") + "-" + curApp.ToString("0000");
                        }
                        txtApplicationNumber.Text = appNo;
                        businessReferenceNumber = "BRN" + appNo;
                        
                        //autocomplete
                        dat.SetActivePage(pageNumber + 1);
                        if (!txtKindOfBusiness.Focused) txtKindOfBusiness.AutoCompleteCustomSource.AddRange(dat.GetStringList(0));
                        
                        dat.SetActivePage(pageNumber + 2);
                        if (!txtBBarangay.Focused) txtBBarangay.AutoCompleteCustomSource.AddRange(dat.GetStringList(0));
                        if (!txtOBarangay.Focused) txtOBarangay.AutoCompleteCustomSource.AddRange(dat.GetStringList(0));

                        btnSave.Enabled = true;
                        //load previous information if renewed                        
                        if (myAction.Equals("RENEWAL") || myAction.Equals("TRANSFER OWNERSHIP") || myAction.Equals("TRANSFER LOCATION")) loadInformation();

                        break;

                    case "LoadInformation":
                        proc.DataFetch(out dat);
                        //load business owner profile
                        dat.SetActivePage(0);
                        if (dat.GetRecordCount(0) == 1)
                        {
                            businessOwner = new updateInfo();
                            businessOwner.fID = dat.GetValue(0, "fID").ToString();
                            businessOwner.fStamp = DateTime.Parse(dat.GetValue(0, "fStamp").ToString());

                            txtFirstName.Text = dat.GetValue(0, "fFirstName").ToString();
                            txtLastName.Text = dat.GetValue(0, "fLastName").ToString();
                            txtMiddleName.Text = dat.GetValue(0, "fMiddleName").ToString();
                            txtCompanyName.Text = dat.GetValue(0, "fCompanyName").ToString();
                            cmbGender.Text = dat.GetValue(0, "fGender").ToString();
                            cmbCivilStatus.Text = dat.GetValue(0, "fCivilStatus").ToString();
                            txtCitizenship.Text = dat.GetValue(0, "fCitizenship").ToString();

                            switch (dat.GetValue(0, "fProfileType").ToString())
                            {
                                case "SINGLE": businessType = "SINGLE"; single.Checked = true; break;
                                case "CORPORATION": businessType = "CORPORATION"; corporation.Checked = true; break;
                                case "PARTNERSHIP": businessType = "PARTNERSHIP"; partnership.Checked = true; break;
                            }

                            txtOHouseNumber.Text = dat.GetValue(0, "fHouseNumber").ToString();
                            txtOBuildingName.Text = dat.GetValue(0, "fBuildingName").ToString();
                            txtOUnitNumber.Text = dat.GetValue(0, "fUnitNumber").ToString();
                            txtOSitio.Text = dat.GetValue(0, "fStreet").ToString();
                            txtOBarangay.Text = dat.GetValue(0, "fBarangay").ToString();
                            txtOSubdivision.Text = dat.GetValue(0, "fSubdivision").ToString();
                            txtOMunicipality.Text = dat.GetValue(0, "fMunicipality").ToString();
                            txtOProvince.Text = dat.GetValue(0, "fProvince").ToString();
                            txtOContactNumber.Text = dat.GetValue(0, "fContactNumber").ToString();
                            txtOEmailAddress.Text = dat.GetValue(0, "fEmailAddress").ToString();
                            //additional for image
                            fImageName = dat.GetValue(0, "fImgName").ToString();
                            
                        }

                        //load business profile 
                        dat.SetActivePage(1);
                        if (dat.GetRecordCount(1) == 1)
                        {
                            businessProfile = new updateInfo();
                            businessProfile.fID = dat.GetValue(0, "fID").ToString();
                            businessProfile.fStamp = DateTime.Parse(dat.GetValue(0, "fStamp").ToString());

                            businessReferenceNumber = dat.GetValue(0, "fBusinessReferenceNumber").ToString();
                            txtKindOfBusiness.Text = dat.GetValue(0, "fKindOfBusiness").ToString();
                            txtBusinessName.Text = dat.GetValue(0, "fBusinessName").ToString();
                            txtTradeNameFranchise.Text = dat.GetValue(0, "fTradeName").ToString();
                            txtBHouseNumber.Text = dat.GetValue(0, "fHouseNumber").ToString();
                            txtBBuildingName.Text = dat.GetValue(0, "fBuildingName").ToString();
                            txtBUnitNumber.Text = dat.GetValue(0, "fUnitNumber").ToString();
                            txtBSitio.Text = dat.GetValue(0, "fStreet").ToString();
                            txtBBarangay.Text = dat.GetValue(0, "fBarangay").ToString();
                            txtBSubdivision.Text = dat.GetValue(0, "fSubdivision").ToString();
                            txtBMunicipality.Text = dat.GetValue(0, "fMunicipality").ToString();
                            txtBProvince.Text = dat.GetValue(0, "fProvince").ToString();
                            txtBContactNumber.Text = dat.GetValue(0, "fContactNumber").ToString();
                            txtBEmailAddress.Text = dat.GetValue(0, "fEmailAddress").ToString();
                            txtPIN.Text = dat.GetValue(0, "fPropertyIndexNumber").ToString();
                            txtBusinessArea.Text = dat.GetValue(0, "fBusinessArea").ToString();
                            txtNEmployeesEstablishment.Value = decimal.Parse(dat.GetValue(0, "fNumberOfEmployeesInEstablishment").ToString());
                            txtNEmployeesTublay.Value = decimal.Parse(dat.GetValue(0, "fNumberOfEmployeesInTublay").ToString());
                            txtLLastName.Text = dat.GetValue(0, "fLessorLastName").ToString();
                            txtLFirstName.Text = dat.GetValue(0, "fLessorFirstName").ToString();
                            txtLMiddleName.Text = dat.GetValue(0, "fLessorMiddleName").ToString();
                            txtLAddress.Text = dat.GetValue(0, "fLessorAddress").ToString();
                            txtLContactNumber.Text = dat.GetValue(0, "fLessorContactNumber").ToString();
                            txtLEmailAddress.Text = dat.GetValue(0, "fLessorEmailAddress").ToString();
                            txtMonthlyRental.Value = decimal.Parse(dat.GetValue(0, "fMonthlyRental").ToString());
                            txtCLastName.Text = dat.GetValue(0, "fContactLastName").ToString();
                            txtCFirstName.Text = dat.GetValue(0, "fContactFirstName").ToString();
                            txtCMiddleName.Text = dat.GetValue(0, "fContactMiddleName").ToString();
                            txtCTelephoneNumber.Text = dat.GetValue(0, "fContactTelephoneNumber").ToString();
                            txtCCellphoneNumber.Text = dat.GetValue(0, "fContactCellphoneNumber").ToString();
                        }

                        //load business information
                        dat.SetActivePage(2);
                        if (dat.GetRecordCount(2) == 1)
                        {
                            businessInformation = new updateInfo();
                            businessInformation.fID = dat.GetValue(0, "fID").ToString();
                            businessInformation.fStamp = DateTime.Parse(dat.GetValue(0, "fStamp").ToString());
                            txtRemarks.Text = dat.GetValue(0, "fRemarks").ToString();

                            if (myAction.Equals("VIEW")) txtDateOfApplication.Text = DateTime.Parse(dat.GetValue(0, "fDateApplied").ToString()).ToLongDateString();
                        }

                        txtReferenceNumber.Text = myReferenceNumber;

                        //load checklist of documents
                        dat.SetActivePage(3);
                        if (dat.GetRecordCount(3) > 0) grdChecklist.DataSource = dat.ExportPage();
                        else { btnMayorApproval.Enabled = false; btnTreasury.Enabled = true; }

                        //control manipulation
                        switch (myAction)
                        {
                            case "TRANSFER LOCATION":
                                ControlTab.Enable_Tab(tabBPAForm.TabPages[1], false);
                                break;
                            case "TRANSFER OWNERSHIP":
                                    grpBusinessInfo.Enabled = false;
                                    grpBusinessAddress.Enabled = false;
                                    chkSame.Enabled = false;
                                    grpAddBusinessInfo.Enabled = false;
                                    grpRented.Enabled = false;
                                    dat.SetActivePage(4);
                                    advanceRefNum = int.Parse(dat.GetValue(0, "fCurNum").ToString());
                                    advanceReferenceInfo.fID = dat.GetValue(0, "fID").ToString();
                                    advanceReferenceInfo.fStamp = DateTime.Parse(dat.GetValue(0, "fStamp").ToString());
                                    btnSave.Enabled = true;
                                    break;
                            case "VIEW": 
                                    txtApplicationNumber.Text = myPreviousApplicationNumber;
                                    ControlTab.Enable_Tab(tabBPAForm.TabPages[1], false);
                                    ControlTab.Enable_Tab(tabBPAForm.TabPages[2], false);
                                    ControlTab.Enable_Tab(tabBPAForm.TabPages[3], false);
                                    break;
                            case "RENEWAL":
                                    grpBusinessInfo.Enabled = false;
                                    //generate advance reference number incase client will transfer ownership
                                    dat.SetActivePage(4);
                                    advanceRefNum = int.Parse(dat.GetValue(0, "fCurNum").ToString());
                                    advanceReferenceInfo.fID = dat.GetValue(0, "fID").ToString();
                                    advanceReferenceInfo.fStamp = DateTime.Parse(dat.GetValue(0, "fStamp").ToString());
                                    break;
                        }
                        

                        //additional
                        if (!fImageName.Equals(""))
                        {
                            hasImage = true;
                            CodeScript s = new CodeScript("GETIMAGE");
                            s.Add(CodeIns.RFU_NEW_CORE);
                            s.Add(CodeIns.RFU_SET_CONFIG, "ftp://" + Common.CFG.Read("SERVER"), Common.CFG.Read("UID"), Common.CFG.Read("PWD"), 256);
                            s.Add(CodeIns.RFU_DWLD, Common.LOCPATH + Const.APPLICATION_EBPLS_IMG, fImageName);
                            s.Add(CodeIns.RFU_END);
                            proc.SetOnErrorBehavior(OnErrorBehavior.ResumeNext);
                            proc.Run(s);
                        }
                        break;
                    case "GETIMAGE":
                        //DataCache cachepriv = new DataCache("img");
                        //proc.DataFetch(out cachepriv);
                        //cachepriv.SetActivePage(0);
                        if (hasImage)
                        {
                            try
                            {
                                using (System.IO.Stream fs = System.IO.File.Open(Common.LOCPATH + Const.APPLICATION_EBPLS_IMG , System.IO.FileMode.Open))
                                {
                                    imgApplicant.Image = new Bitmap(fs);
                                    fs.Close();
                                }
                            }
                            catch (Exception)
                            {
                                Confirm.Error("User Image", "There was a problem loading your user image.");
                                imgApplicant.Image = new Bitmap(480, 480);
                            }
                        }
                        break;
                    case "GEN_APPNO":
                        proc.DataFetch(out dat);
                        curApp = 0;
                        appNo = "";
                        
                        //Set the active page before exporting
                        dat.SetActivePage(0);
                        if (dat.GetRecordCount(0) == 1)
                        {
                            isExistingDate = true;
                            applicationNumberInfo.fID = dat.GetValue(0, "fID").ToString();
                            applicationNumberInfo.fStamp = DateTime.Parse(dat.GetValue(0, "fStamp").ToString());

                            curApp = int.Parse(dat.GetValue(0, "fCurNum").ToString());
                            appNo = txtDateOfApplication.Value.ToString("yyyy-MM-dd") + "-" + curApp.ToString("0000");
                        }
                        else
                        {
                            isExistingDate = false;
                            appNo = txtDateOfApplication.Value.ToString("yyyy-MM-dd") + "-" + curApp.ToString("0000");
                        }
                        txtApplicationNumber.Text = appNo;
                        break;
                    case "TRANSFER OWNERSHIP":
                    case "TRANSFER LOCATION":
                    case "RENEWAL":
                    case "COMMIT":



                        btnClose_Click(null, null);
                        break;

                    case "CheckAndGet":
                        proc.DataFetch(out dat);
                        dat.SetActivePage(0);
                        
                            if (dat.GetRecordCount(0) == 1)
                            {
                                int curDBNum = int.Parse(dat.GetValue(0, "fReferenceNumber").ToString());
                                txtReferenceNumber.Text = curDBNum.ToString("0000000");
                                lblErrorMsg.Text = "Warning Message!!! \r\nExisting Record Found!!!\r\nExisting Reference Number\r\nwill be used";
                                txtReferenceNumber.BackColor = Color.Yellow;

                                advanceBusinessOwner = new updateInfo();
                                advanceBusinessOwner.fID = dat.GetValue(0, "fID").ToString();
                                advanceBusinessOwner.fStamp = DateTime.Parse(dat.GetValue(0, "fStamp").ToString());

                                cmbGender.Text = dat.GetValue(0, "fGender").ToString();
                                cmbCivilStatus.Text = dat.GetValue(0, "fCivilStatus").ToString();
                                txtCitizenship.Text = dat.GetValue(0, "fCitizenship").ToString();

                                switch (dat.GetValue(0, "fProfileType").ToString())
                                {
                                    case "SINGLE": businessType = "SINGLE"; single.Checked = true; break;
                                    case "CORPORATION": businessType = "CORPORATION"; corporation.Checked = true; break;
                                    case "PARTNERSHIP": businessType = "PARTNERSHIP"; partnership.Checked = true; break;
                                }

                                txtOHouseNumber.Text = dat.GetValue(0, "fHouseNumber").ToString();
                                txtOBuildingName.Text = dat.GetValue(0, "fBuildingName").ToString();
                                txtOUnitNumber.Text = dat.GetValue(0, "fUnitNumber").ToString();
                                txtOSitio.Text = dat.GetValue(0, "fStreet").ToString();
                                txtOBarangay.Text = dat.GetValue(0, "fBarangay").ToString();
                                txtOSubdivision.Text = dat.GetValue(0, "fSubdivision").ToString();
                                txtOMunicipality.Text = dat.GetValue(0, "fMunicipality").ToString();
                                txtOProvince.Text = dat.GetValue(0, "fProvince").ToString();
                                txtOContactNumber.Text = dat.GetValue(0, "fContactNumber").ToString();
                                txtOEmailAddress.Text = dat.GetValue(0, "fEmailAddress").ToString();
                                isExisitngOwnerProfile = true;
                                colorControl(Color.Yellow);
                            }
                            else
                            {
                                txtReferenceNumber.BackColor = Color.Green;
                                txtReferenceNumber.Text = currentRefNum.ToString("0000000");
                                lblErrorMsg.Text = "";
                                btnSave.Enabled = true;
                                clearAddress();
                                isExisitngOwnerProfile = false;
                                colorControl(Color.Green);
                                if (myAction.Equals("RENEWAL") || myAction.Equals("TRANSFER OWNERSHIP")) txtReferenceNumber.Text = advanceRefNum.ToString("0000000");
                            }
                       
                        if ((!isExisitngOwnerProfile && myAction.Equals("RENEWAL")) || (!isExisitngOwnerProfile && myAction.Equals("TRANSFER OWNERSHIP"))) txtReferenceNumber.Text = advanceRefNum.ToString("0000000");
                        break;
                }
            });    
        }
        
        //check if there are list of documents approved 
        void checkCheckList()
        {
            if (grdChecklist.Rows.Count > 0)
            {
                btnTreasury.Enabled = true;
            }
            else
            {
                btnTreasury.Enabled = false;
            }
        }
        //method for single and company
        void grpEnabling(bool s)
        {
            grpCompany.Enabled = s;
            grpSingle.Enabled = !s;
        }
        //same address
        void theSameWithOwnerAddress()
        {
            txtBHouseNumber.Text = txtOHouseNumber.Text;
            txtBBuildingName.Text = txtOBuildingName.Text;
            txtBUnitNumber.Text = txtOUnitNumber.Text;
            txtBSitio.Text = txtOSitio.Text;
            txtBBarangay.Text = txtOBarangay.Text;
            txtBSubdivision.Text = txtOSubdivision.Text;
            txtBMunicipality.Text = txtOMunicipality.Text;
            txtBProvince.Text = txtOProvince.Text;
            txtBContactNumber.Text = txtOContactNumber.Text;
            txtBEmailAddress.Text = txtOEmailAddress.Text;
        }
        //method for color control
        void colorControl(Color c)
        {
           
            txtOHouseNumber.BackColor = c;
            txtOBuildingName.BackColor = c;
            txtOUnitNumber.BackColor = c;
            txtOSitio.BackColor = c;
            txtOBarangay.BackColor = c;
            txtOSubdivision.BackColor = c;
            txtOMunicipality.BackColor = c;
            txtOProvince.BackColor = c;
            txtOContactNumber.BackColor = c;
            txtOEmailAddress.BackColor = c;
        }

        #region Initialization and Loading of Information
        //method for loading information
        void loadInformation()
        {
            //Check if the processor is executing script, if it is, return to the calling code.
            if (proc.State != ProcessorFlag.Ready && proc.State != ProcessorFlag.DataRdy) return;

            string sysDate = txtDateOfApplication.Value.ToString("yyyy-MM-dd");
            script = new CodeScript("LoadInformation");
            script.Add(CodeIns.SYS_RESET);
            script.Add(CodeIns.DBU_NEW_CORE);
            script.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"),
                       Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
            script.Add(CodeIns.DBU_BEGIN_TRANSACTION);
            script.Add(CodeIns.DBU_QUERY, "SELECT * FROM tblBusinessOwnerProfile WHERE fReferenceNumber = '"+myReferenceNumber+"'");
            script.Add(CodeIns.DBU_QUERY, "SELECT * FROM tblBusinessProfile WHERE fBusinessReferenceNumber = '" + myBusinessReferenceNumber + "'");
            script.Add(CodeIns.DBU_QUERY, "SELECT * FROM tblBusinessInformation WHERE fBusinessReferenceNumber = '" + myBusinessReferenceNumber + "'"+
                                            "AND fApplicationNumber = '" + myPreviousApplicationNumber + "'");
            script.Add(CodeIns.DBU_QUERY, "SELECT fDescription AS 'DESCRIPTION', fResponsibleOffice AS 'RESPONSIBLE OFFICE', fIssuedBy AS 'ISSUED BY', fDateIssued AS 'DATE ISSUED',fStatus AS 'Application Status', fOtherRemarks AS 'OTHER REMARKS' FROM tblDocumentsCheckList WHERE fApplicationNumber = '"+myPreviousApplicationNumber+"'");
            script.Add(CodeIns.DBU_QUERY, "SELECT * FROM tblGeneratedRefNum");
            script.Add(CodeIns.DBU_END);
            proc.Run(script);
        }
        //method for new business permit
        void initializeGrid()
        {
            //Check if the processor is executing script, if it is, return to the calling code.
            if (proc.State != ProcessorFlag.Ready && proc.State != ProcessorFlag.DataRdy) return;

            string sysDate = txtDateOfApplication.Value.ToString("yyyy-MM-dd");
            script = new CodeScript("INIT_GRID");
            script.Add(CodeIns.SYS_RESET);
            script.Add(CodeIns.DBU_NEW_CORE);
            script.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"),
                       Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
            script.Add(CodeIns.DBU_BEGIN_TRANSACTION);
            
            if (myAction.Equals("") || isOnError)
            {
                script.Add(CodeIns.DBU_QUERY, "SELECT * FROM tblGeneratedRefNum");
            }
            
            script.Add(CodeIns.DBU_QUERY, "SELECT * FROM tblGeneratedAppNum WHERE fDate = '" + sysDate + "'");
            script.Add(CodeIns.DBU_QUERY, "SELECT fDescription FROM tblLineOfBusiness");
            script.Add(CodeIns.DBU_QUERY, "SELECT fData FROM tbllookup WHERE fKey = 'BARANGAY'");
            script.Add(CodeIns.DBU_END);
            proc.Run(script);
        }
        //use only if date is altered
        void generateAppNo()
        {
            //Check if the processor is executing script, if it is, return to the calling code.
            if (proc.State != ProcessorFlag.Ready && proc.State != ProcessorFlag.DataRdy) return;

            string sysDate = txtDateOfApplication.Value.ToString("yyyy-MM-dd");
            script = new CodeScript("GEN_APPNO");
            script.Add(CodeIns.SYS_RESET);
            script.Add(CodeIns.DBU_NEW_CORE);
            script.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"),
                       Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
            script.Add(CodeIns.DBU_BEGIN_TRANSACTION);
            script.Add(CodeIns.DBU_QUERY, "SELECT * FROM tblGeneratedAppNum WHERE fDate = '" + sysDate + "'");
            script.Add(CodeIns.DBU_END);
            proc.Run(script);

        }
        #endregion

        #region Checking Business Name and Profile
        //checking for business owner profile
        void checkAndGetBusinessOwnerProfile()
        {
            //Check if the processor is executing script, if it is, return to the calling code.
            if (proc.State != ProcessorFlag.Ready && proc.State != ProcessorFlag.DataRdy) return;

            script = new CodeScript("CheckAndGet");
            script.Add(CodeIns.SYS_RESET);
            script.Add(CodeIns.DBU_NEW_CORE);
            script.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"),
                       Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
            script.Add(CodeIns.DBU_BEGIN_TRANSACTION);
            if(businessType.Equals("SINGLE"))
                script.Add(CodeIns.DBU_QUERY, "SELECT * FROM tblBusinessOwnerProfile WHERE fLastName = '" + txtLastName.Text.Trim() + "' AND fFirstName = '" + txtFirstName.Text.Trim() + "' AND fMiddleName = '" + txtMiddleName.Text.Trim() + "'");
            else
                script.Add(CodeIns.DBU_QUERY, "SELECT * FROM tblBusinessOwnerProfile WHERE fCompanyName = '" + txtCompanyName.Text.Trim() + "'");
            script.Add(CodeIns.DBU_QUERY, "SELECT * FROM tblGeneratedRefNum");

            script.Add(CodeIns.DBU_END);
            proc.Run(script);
        }
        //checking business name and tradename 
        void checkBusinessNameTradeName()
        {
            //Check if the processor is executing script, if it is, return to the calling code.
            if (proc.State != ProcessorFlag.Ready && proc.State != ProcessorFlag.DataRdy) return;
            string businessName = txtBusinessName.Text.Trim();

            script = new CodeScript("CheckBusinessName");
            script.Add(CodeIns.SYS_RESET);
            script.Add(CodeIns.DBU_NEW_CORE);
            script.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"),
                       Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
            script.Add(CodeIns.DBU_BEGIN_TRANSACTION);
            script.Add(CodeIns.DBU_QUERY, "SELECT * FROM tblBusinessProfile WHERE (fBusinessName = '" + businessName + "' AND fBusinessStatus = 'APPROVED')");
            script.Add(CodeIns.DBU_END);
            proc.Run(script);
        }
        #endregion

        #region Printing Method
        void PrintOath()
        {

            PrintDocument pd = new PrintDocument();
            pd.DocumentName = lblText.Text;
            pd.DefaultPageSettings.PaperSize = new PaperSize("Folio", 850, 1300);
            pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
            PrintDialog pdlg = new PrintDialog();
            pdlg.Document = pd;
            if (pdlg.ShowDialog() == DialogResult.OK)
            {
                PrintPreviewDialog ppdlg = new PrintPreviewDialog();
                ppdlg.Document = pd;
                ppdlg.ShowDialog();
            }
        }
        void pd_PrintPage(object sender, PrintPageEventArgs e)
        {
            float lpp = 0;
            float y = 0;
            float tm = e.MarginBounds.Top;
            float lm = e.PageBounds.Left + 50;
            float lw = e.PageBounds.Width - 50;
            float th = e.PageBounds.Height - 100;
            float tHt = 0;
            Font cFont;
            StringFormat sf = new StringFormat();
            //Title
            cFont = new Font("Arial", 10f, FontStyle.Regular);
            tHt = cFont.GetHeight(e.Graphics);
            y = 50;
            e.Graphics.DrawImage(pictureBox2.Image, new Rectangle(100,35,100,100));
            sf.Alignment = StringAlignment.Center;
            e.Graphics.DrawString("Republic of the Philippines", cFont, Brushes.Black, (lw + 100) / 2, y, sf);
            y += tHt;
            e.Graphics.DrawString("PROVINCE OF BENGUET", cFont, Brushes.Black, (lw + 100) / 2, y, sf);
            y += tHt;
            e.Graphics.DrawString("T U B L A Y", cFont, Brushes.Black, (lw + 100) / 2, y, sf);

            y += tHt;
            y += tHt;
            //SubTitle
            cFont = new Font("Arial", 10f, FontStyle.Bold);
            e.Graphics.DrawString("APPLICATION FORM FOR BUSINESS LICENSE (BUSINESS PERMIT)", cFont, Brushes.Black, (lw + 100) / 2, y, sf);

            cFont = new Font("Arial", 10f, FontStyle.Italic);
            y += tHt;
            e.Graphics.DrawString("TAX YEAR "+DateTime.Now.Year, cFont, Brushes.Black, (lw + 100) / 2, y, sf);

            //Line
            y += tHt;
            y += tHt;
            e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lm, y), new PointF(lw, y));
            
            //Personal Information
            y += tHt;
            y += tHt;
            cFont = new Font("Arial", 11f, FontStyle.Regular);
            tHt = cFont.GetHeight(e.Graphics);
            e.Graphics.DrawString("Date of Application: "+txtDateOfApplication.Value.ToString("yyyy-MM-dd"), cFont, Brushes.Black, lm + 20, y, new StringFormat());
            e.Graphics.DrawString("Application Number: "+txtApplicationNumber.Text , cFont, Brushes.Black, lm + 380, y, new StringFormat());
            
            y += tHt;
            y += tHt;
            e.Graphics.DrawString("Reference Number: " + txtReferenceNumber.Text, cFont, Brushes.Black, lm + 20, y, new StringFormat());
            e.Graphics.DrawString("Kind of Organization: " + businessType, cFont, Brushes.Black, lm + 380, y, new StringFormat());

            if (businessType.Equals("SINGLE"))
            {
                y += tHt;
                e.Graphics.DrawString("Last Name: " + txtLastName.Text, cFont, Brushes.Black, lm + 20, y, new StringFormat());
                e.Graphics.DrawString("Citizenship: " + txtCitizenship.Text, cFont, Brushes.Black, lm + 380, y, new StringFormat());

                y += tHt;
                e.Graphics.DrawString("First Name: " + txtFirstName.Text, cFont, Brushes.Black, lm + 20, y, new StringFormat());
                e.Graphics.DrawString("Civil Status: " + cmbCivilStatus.Text, cFont, Brushes.Black, lm + 380, y, new StringFormat());

                y += tHt;
                e.Graphics.DrawString("Middle Name: " + txtMiddleName.Text, cFont, Brushes.Black, lm + 20, y, new StringFormat());
                e.Graphics.DrawString("Gender: " + cmbGender.Text, cFont, Brushes.Black, lm + 380, y, new StringFormat());
            }
            else
            {
                y += tHt;
                e.Graphics.DrawString("Company Name: " + txtCompanyName.Text, cFont, Brushes.Black, lm + 20, y, new StringFormat());
                
            }

            cFont = new Font("Arial", 11f, FontStyle.Bold);
            tHt = cFont.GetHeight(e.Graphics);
            y += tHt;
            y += tHt;
            e.Graphics.DrawString("OWNER'S ADDRESS", cFont, Brushes.Black, lm + 20, y, new StringFormat());
            e.Graphics.DrawString("BUSINESS ADDRESS " , cFont, Brushes.Black, lm + 380, y, new StringFormat());

            cFont = new Font("Arial", 11f, FontStyle.Regular);
            tHt = cFont.GetHeight(e.Graphics);
            y += tHt;
            y += tHt;
            e.Graphics.DrawString("House No/Bldg No: "+txtOHouseNumber.Text, cFont, Brushes.Black, lm + 20, y, new StringFormat());
            e.Graphics.DrawString("House No/Bldg No: "+txtBHouseNumber.Text, cFont, Brushes.Black, lm + 380, y, new StringFormat());

            y += tHt;
            e.Graphics.DrawString("Building Name: " + txtOBuildingName.Text, cFont, Brushes.Black, lm + 20, y, new StringFormat());
            e.Graphics.DrawString("Building Name: " + txtBBuildingName.Text, cFont, Brushes.Black, lm + 380, y, new StringFormat());

            y += tHt;
            e.Graphics.DrawString("Unit No: " + txtOUnitNumber.Text, cFont, Brushes.Black, lm + 20, y, new StringFormat());
            e.Graphics.DrawString("Unit No: " + txtBUnitNumber.Text, cFont, Brushes.Black, lm + 380, y, new StringFormat());

            y += tHt;
            e.Graphics.DrawString("Street/ Sitio: " + txtOSitio.Text, cFont, Brushes.Black, lm + 20, y, new StringFormat());
            e.Graphics.DrawString("Street/ Sitio: " + txtBSitio.Text, cFont, Brushes.Black, lm + 380, y, new StringFormat());

            y += tHt;
            e.Graphics.DrawString("Barangay: " + txtOBarangay.Text, cFont, Brushes.Black, lm + 20, y, new StringFormat());
            e.Graphics.DrawString("Barangay: " + txtBBarangay.Text, cFont, Brushes.Black, lm + 380, y, new StringFormat());

            y += tHt;
            e.Graphics.DrawString("Subdivision: " + txtOSubdivision.Text, cFont, Brushes.Black, lm + 20, y, new StringFormat());
            e.Graphics.DrawString("Subdivision: " + txtBSubdivision.Text, cFont, Brushes.Black, lm + 380, y, new StringFormat());

            y += tHt;
            e.Graphics.DrawString("Municipality: " + txtOMunicipality.Text, cFont, Brushes.Black, lm + 20, y, new StringFormat());
            e.Graphics.DrawString("Municipality: " + txtBMunicipality.Text, cFont, Brushes.Black, lm + 380, y, new StringFormat());

            y += tHt;
            e.Graphics.DrawString("Province: " + txtOProvince.Text, cFont, Brushes.Black, lm + 20, y, new StringFormat());
            e.Graphics.DrawString("Province: " + txtBProvince.Text, cFont, Brushes.Black, lm + 380, y, new StringFormat());

            y += tHt;
            e.Graphics.DrawString("Telephone No: " + txtOContactNumber.Text, cFont, Brushes.Black, lm + 20, y, new StringFormat());
            e.Graphics.DrawString("Telephone No: " + txtBContactNumber.Text, cFont, Brushes.Black, lm + 380, y, new StringFormat());

            y += tHt;
            e.Graphics.DrawString("Email Address: " + txtOEmailAddress.Text, cFont, Brushes.Black, lm + 20, y, new StringFormat());
            e.Graphics.DrawString("Email Address: " + txtBEmailAddress.Text, cFont, Brushes.Black, lm + 380, y, new StringFormat());
            
            y += tHt;
            y += tHt;
            e.Graphics.DrawString("Property Index Number (PIN): " + txtPIN.Text, cFont, Brushes.Black, lm + 20, y, new StringFormat());
            e.Graphics.DrawString("Business Area (in sq. m.): " + txtBusinessArea.Text, cFont, Brushes.Black, lm + 380, y, new StringFormat());

            y += tHt;
            e.Graphics.DrawString("Total No. of Employees in Establishment: " + txtNEmployeesEstablishment.Text, cFont, Brushes.Black, lm + 20, y, new StringFormat());
            e.Graphics.DrawString("No. of Employees in Tublay: " + txtNEmployeesTublay.Text, cFont, Brushes.Black, lm + 380, y, new StringFormat());


           
            if (txtLLastName.Text != "" && txtLFirstName.Text != "" && txtLMiddleName.Text != "")
            {
                cFont = new Font("Arial", 11f, FontStyle.Bold);
                tHt = cFont.GetHeight(e.Graphics);
                y += tHt;
                y += tHt;
                e.Graphics.DrawString("IF PLACE OF BUSINESS IS RENTED, PLEASE IDENTIFY THE FOLLOWING", cFont, Brushes.Black, (lw + 50) / 2, y, sf);
                y += tHt;
                e.Graphics.DrawString("LESSOR'S NAME", cFont, Brushes.Black, lm + 20, y, new StringFormat());
                

                cFont = new Font("Arial", 11f, FontStyle.Regular);
                tHt = cFont.GetHeight(e.Graphics);
                y += tHt;
                e.Graphics.DrawString("Last Name: " + txtLLastName.Text, cFont, Brushes.Black, lm + 20, y, new StringFormat());
                e.Graphics.DrawString("Telephone No: " + txtLContactNumber.Text, cFont, Brushes.Black, lm + 380, y, new StringFormat());

                y += tHt;
                e.Graphics.DrawString("First Name: " + txtLFirstName.Text, cFont, Brushes.Black, lm + 20, y, new StringFormat());
                e.Graphics.DrawString("Email Address: " + txtLEmailAddress.Text, cFont, Brushes.Black, lm + 380, y, new StringFormat());

                y += tHt;
                e.Graphics.DrawString("Middle Name: " + txtLMiddleName.Text, cFont, Brushes.Black, lm + 20, y, new StringFormat());
                e.Graphics.DrawString("Monthly Rental: " + txtMonthlyRental.Text, cFont, Brushes.Black, lm + 380, y, new StringFormat());

                y += tHt;
                string address = txtLAddress.Text;
                int cnt = 0;
                if (address.Length > 60)
                {
                    cnt = (int)address.Length / 60;
                    e.Graphics.DrawString("Lessor's Address: " + address.Substring(0, 60), cFont, Brushes.Black, lm + 20, y, new StringFormat());
                    for (int i = 1; i < cnt; i++)
                    {
                        y += tHt;
                        e.Graphics.DrawString("               " + address.Substring((i * 60) + 1, 60), cFont, Brushes.Black, lm + 20, y, new StringFormat());
                    }
                }
                else
                {
                    e.Graphics.DrawString("Lessor's Address: " + address, cFont, Brushes.Black, lm + 20, y, new StringFormat());
                }

            }

            cFont = new Font("Arial", 11f, FontStyle.Bold);
            tHt = cFont.GetHeight(e.Graphics);
            y += tHt;
            y += tHt;
            e.Graphics.DrawString("CONTACT PERSON INCASE OF EMERGENCY", cFont, Brushes.Black, (lw + 50) / 2, y, sf);

            cFont = new Font("Arial", 11f, FontStyle.Regular);
            tHt = cFont.GetHeight(e.Graphics);
            y += tHt;
            e.Graphics.DrawString("Last Name: " + txtCLastName.Text, cFont, Brushes.Black, lm + 20, y, new StringFormat());
            e.Graphics.DrawString("Telephone No: " + txtCTelephoneNumber.Text, cFont, Brushes.Black, lm + 380, y, new StringFormat());

            y += tHt;
            e.Graphics.DrawString("First Name: " + txtCFirstName.Text, cFont, Brushes.Black, lm + 20, y, new StringFormat());
            e.Graphics.DrawString("Cellphone No: " + txtCCellphoneNumber.Text, cFont, Brushes.Black, lm + 380, y, new StringFormat());

            y += tHt;
            e.Graphics.DrawString("Middle Name: " + txtCMiddleName.Text, cFont, Brushes.Black, lm + 20, y, new StringFormat());

            y += tHt;
            y += tHt;
            cFont = new Font("Arial", 11f, FontStyle.Bold);
            tHt = cFont.GetHeight(e.Graphics);
            y += tHt;
            y += tHt;
            e.Graphics.DrawString("OATH OF UNDERTAKING", cFont, Brushes.Black, (lw + 50) / 2, y, sf);

            cFont = new Font("Arial", 10f, FontStyle.Regular);
            tHt = cFont.GetHeight(e.Graphics);
            y += tHt;
            y += tHt;
            e.Graphics.DrawString(oath1.Text, cFont, Brushes.Black, lm + 20, y, new StringFormat());
            y += tHt;
            e.Graphics.DrawString(oath2.Text, cFont, Brushes.Black, lm + 20, y, new StringFormat());
            y += tHt;
            e.Graphics.DrawString(oath3.Text, cFont, Brushes.Black, lm + 20, y, new StringFormat());
            y += tHt;
            e.Graphics.DrawString(oath3a.Text, cFont, Brushes.Black, lm + 20, y, new StringFormat());
            y += tHt;
            e.Graphics.DrawString(oath4.Text, cFont, Brushes.Black, lm + 20, y, new StringFormat());
            y += tHt;
            e.Graphics.DrawString(oath5.Text, cFont, Brushes.Black, lm + 20, y, new StringFormat());
            y += tHt;
            e.Graphics.DrawString(oath5a.Text, cFont, Brushes.Black, lm + 20, y, new StringFormat());
            y += tHt;
            e.Graphics.DrawString(oath6.Text, cFont, Brushes.Black, lm + 20, y, new StringFormat());
            y += tHt;
            e.Graphics.DrawString(oath7.Text, cFont, Brushes.Black, lm + 20, y, new StringFormat());
            y += tHt;
            e.Graphics.DrawString(oath7a.Text, cFont, Brushes.Black, lm + 20, y, new StringFormat());
            y += tHt;
            y += tHt;
            e.Graphics.DrawString("_________________________", cFont, Brushes.Black, lm + 20, y, new StringFormat());
            e.Graphics.DrawString("_________________________", cFont, Brushes.Black, lm + 280, y, new StringFormat());
            e.Graphics.DrawString("_________________________", cFont, Brushes.Black, lm + 500, y, new StringFormat());
            y += tHt;
            e.Graphics.DrawString("(SIGNATURE OF APPLICANT)", cFont, Brushes.Black, lm + 20, y, new StringFormat());
            e.Graphics.DrawString("(POSITION / TITLE)", cFont, Brushes.Black, lm + 320, y, new StringFormat());
            e.Graphics.DrawString("(DATE SIGNED)", cFont, Brushes.Black, lm + 550, y, new StringFormat());
            y += tHt;
            y += tHt;
            e.Graphics.DrawString(oath8.Text, cFont, Brushes.Black, lm + 20, y, new StringFormat());
            y += tHt;
            e.Graphics.DrawString(oath9.Text, cFont, Brushes.Black, lm + 20, y, new StringFormat());
            y += tHt;
            e.Graphics.DrawString(oath9a.Text, cFont, Brushes.Black, lm + 20, y, new StringFormat());

        }
        #endregion

        #region Commit Methods
        //used for saving new information
        void commitNewBPLInformation()
        {
            //Check if the processor is executing script, if it is, return to the calling code.
            if (proc.State != ProcessorFlag.Ready && proc.State != ProcessorFlag.DataRdy) return;

            string sysDate = txtDateOfApplication.Value.ToString("yyyy-MM-dd");
            script = new CodeScript("COMMIT");
            script.Add(CodeIns.SYS_RESET);
            script.Add(CodeIns.DBU_NEW_CORE);
            script.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"),
                       Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
            script.Add(CodeIns.DBU_BEGIN_TRANSACTION);

            //application number
            if (isExistingDate)
                script.Add(CodeIns.DBU_UPDATE, "tblGeneratedAppNum", new string[] { "fCurNum" }, new object[] { "fCurNum+1" }, "fID='" + applicationNumberInfo.fID + "' AND fStamp = '" + applicationNumberInfo.fStamp.ToString("yyyy-MM-dd HH:mm:ss") + "'");
            else
                script.Add(CodeIns.DBU_INSERT, "tblGeneratedAppNum", new string[] { "fDate", "fCurNum" }, new object[] { "'" + sysDate + "'", "1" });

            //business owner profile
            if (myAction.Equals(""))
            {
                //reference number
                script.Add(CodeIns.DBU_UPDATE, "tblGeneratedRefNum", new string[] { "fCurNum" }, new object[] { "fCurNum+1" }, "fID='" + referenceNumberInfo.fID + "' AND fStamp = '" + referenceNumberInfo.fStamp.ToString("yyyy-MM-dd HH:mm:ss") + "'");

                if (!isExisitngOwnerProfile)
                {
                    //additional for applicant image
                    object imgName = "''";
                    if (hasImage) imgName = Sym.PQS(Common.CFG.Read("DB") + "/tblapplication/" + txtReferenceNumber.Text);

                    script.Add(CodeIns.DBU_INSERT, "tblBusinessOwnerProfile",
                        new string[] 
                    {   "fRegistrationDate",    "fReferenceNumber",     "fCompanyName",     "fLastName",        "fFirstName",       "fMiddleName",
                        "fCitizenship",         "fCivilStatus",         "fGender",          "fHouseNumber",     "fBuildingName",    "fUnitNumber",
                        "fStreet",              "fBarangay",            "fSubdivision",     "fMunicipality",    "fProvince",        "fContactNumber",
                        "fEmailAddress",        "fProfileType",         "fImgName"
                    },
                        new object[] 
                    {   "'" + txtDateOfApplication.Value.ToString("yyyy-MM-dd")+ "'",
                        "'" + txtReferenceNumber.Text.Trim() + "'",
                        "'" + txtCompanyName.Text.Trim() + "'",
                        "'" + txtLastName.Text.Trim() + "'",
                        "'" + txtFirstName.Text.Trim() + "'",
                        "'" + txtMiddleName.Text.Trim() + "'",

                        "'" + txtCitizenship.Text.Trim() + "'",
                        "'" + cmbCivilStatus.Text + "'",
                        "'" + cmbGender.Text + "'",
                        "'" + txtOHouseNumber.Text.Trim() + "'",
                        "'" + txtOBuildingName.Text.Trim() + "'",
                        "'" + txtOUnitNumber.Text.Trim() + "'",
                    
                        "'" + txtOSitio.Text.Trim() + "'",
                        "'" + txtOBarangay.Text.Trim() + "'",
                        "'" + txtOSubdivision.Text.Trim() + "'",
                        "'" + txtOMunicipality.Text.Trim() + "'",
                        "'" + txtOProvince.Text.Trim() + "'",
                        "'" + txtOContactNumber.Text.Trim() + "'",
                    
                        "'" + txtOEmailAddress.Text.Trim() + "'",
                        "'" + businessType + "'",
                        imgName
                    });
                }
            }
                //business profile
                script.Add(CodeIns.DBU_INSERT, "tblbusinessprofile",
                    new string[]
                {
                    "fDateApplied",                 "fBusinessReferenceNumber",     "fBusinessType",            "fKindOfOrganization",      "fBusinessName",
                    "fKindOfBusiness",              "fTradeName",                   "fHouseNumber",             "fBuildingName",            "fUnitNumber",
                    "fStreet",                      "fBarangay",                    "fSubdivision",             "fMunicipality",            "fProvince",
                    "fContactNumber",               "fEmailAddress",                "fPropertyIndexNumber",     "fBusinessArea",            "fNumberOfEmployeesInEstablishment",
                    "fNumberOfEmployeesInTublay",   "fLessorLastName",              "fLessorFirstName",         "fLessorMiddleName",        "fLessorAddress",
                    "fLessorContactNumber",         "fLessorEmailAddress",          "fMonthlyRental",           "fContactLastName",         "fContactFirstName",
                    "fContactMiddleName",           "fContactTelephoneNumber",      "fContactCellphoneNumber",  "fBusinessStatus",          "fReferenceNumber"
                },
                    new object[]
                {
                    "'" + txtDateOfApplication.Value.ToString("yyyy-MM-dd")+ "'",   
                    "'" + businessReferenceNumber + "'",
                    "'" + businessType + "'",
                    "'" + txtKindOfOrganization.Text.Trim() + "'",
                    "'" + txtBusinessName.Text.Trim() + "'",
                    "'" + txtKindOfBusiness.Text.Trim() + "'",
                    "'" + txtTradeNameFranchise.Text.Trim() + "'",
                    "'" + txtBHouseNumber.Text.Trim() + "'",
                    "'" + txtBBuildingName.Text.Trim() + "'",
                    "'" + txtBUnitNumber.Text.Trim() + "'",
                    "'" + txtBSitio.Text.Trim() + "'",
                    "'" + txtBBarangay.Text.Trim() + "'",
                    "'" + txtBSubdivision.Text.Trim() + "'",
                    "'" + txtBMunicipality.Text.Trim() + "'",
                    "'" + txtBProvince.Text.Trim() + "'",
                    "'" + txtBContactNumber.Text.Trim() + "'",
                    "'" + txtBEmailAddress.Text.Trim() + "'",
                    "'" + txtPIN.Text.Trim() + "'",
                    "'" + txtBusinessArea.Text.Trim() + "'",
                    "'" + txtNEmployeesEstablishment.Value.ToString() + "'",
                    "'" + txtNEmployeesTublay.Value.ToString() + "'",
                    "'" + txtLLastName.Text.Trim() + "'",
                    "'" + txtLFirstName.Text.Trim() + "'",
                    "'" + txtLMiddleName.Text + "'",
                    "'" + txtLAddress.Text.Trim() + "'",
                    "'" + txtLContactNumber.Text.Trim() + "'",
                    "'" + txtLEmailAddress.Text.Trim() + "'",
                    "'" + float.Parse(txtMonthlyRental.Value.ToString()).ToString() + "'",
                    "'" + txtCLastName.Text.Trim() + "'",
                    "'" + txtCFirstName.Text.Trim() + "'",
                    "'" + txtCMiddleName.Text.Trim() + "'",
                     "'" + txtCTelephoneNumber.Text.Trim() + "'",
                    "'" + txtCCellphoneNumber.Text.Trim() + "'",
                    "'PENDING'", "'"+txtReferenceNumber.Text.Trim()+"'"
                });
            
            //application form information
            script.Add(CodeIns.DBU_INSERT, "tblbusinessinformation",
                new string[]
                {
                    "fDateApplied",         "fBusinessReferenceNumber",     "fReferenceNumber",     "fApplicationNumber",       "fBusinessType",
                    "fKindOfOrganization",  "fBusinessStatus",              "fBusinessName",        "fKindOfBusiness",          "fTradeName",
                    "fHouseNumber",
                    "fBuildingName",
                    "fUnitNumber",
                    "fStreet",
                    "fBarangay",
                    "fSubdivision",
                    "fMunicipality",
                    "fProvince",
                    "fContactNumber",
                    "fEmailAddress",
                    "fPropertyIndexNumber",
                    "fBusinessArea",
                    "fNumberOfEmployeesInEstablishment",
                    "fNumberOfEmployeesInTublay",
                    "fLessorLastName",
                    "fLessorFirstName",
                    "fLessorMiddleName",
                    "fLessorAddress",
                    "fLessorContactNumber",
                    "fLessorEmailAddress",
                    "fMonthlyRental",
                    "fContactLastName",
                    "fContactFirstName",
                    "fContactMiddleName",
                    "fContactTelephoneNumber",
                    "fContactCellphoneNumber",
                    "fRemarks"
                },
                new object[]
                {
                    "'" + txtDateOfApplication.Value.ToString("yyyy-MM-dd")+ "'",
                    "'" + businessReferenceNumber + "'",
                    "'" + txtReferenceNumber.Text.Trim() + "'",
                    "'" + txtApplicationNumber.Text.Trim() + "'",
                    "'" + businessType + "'",
                    "'" + txtKindOfOrganization.Text.Trim() + "'",
                    "'PENDING'",
                    "'" + txtBusinessName.Text.Trim() + "'",
                    "'" + txtKindOfBusiness.Text.Trim() + "'",
                    "'" + txtTradeNameFranchise.Text.Trim() + "'",
                    "'" + txtBHouseNumber.Text.Trim() + "'",
                    "'" + txtBBuildingName.Text.Trim() + "'",
                    "'" + txtBUnitNumber.Text.Trim() + "'",
                    "'" + txtBSitio.Text.Trim() + "'",
                    "'" + txtBBarangay.Text.Trim() + "'",
                    "'" + txtBSubdivision.Text.Trim() + "'",
                    "'" + txtBMunicipality.Text.Trim() + "'",
                    "'" + txtBProvince.Text.Trim() + "'",
                    "'" + txtBContactNumber.Text.Trim() + "'",
                    "'" + txtBEmailAddress.Text.Trim() + "'",
                    "'" + txtPIN.Text.Trim() + "'",
                    "'" + txtBusinessArea.Text.Trim() + "'",
                    "'" + txtNEmployeesEstablishment.Value.ToString() + "'",
                    "'" + txtNEmployeesTublay.Value.ToString() + "'",
                    "'" + txtLLastName.Text.Trim() + "'",
                    "'" + txtLFirstName.Text.Trim() + "'",
                    "'" + txtLMiddleName.Text + "'",
                    "'" + txtLAddress.Text.Trim() + "'",
                    "'" + txtLContactNumber.Text.Trim() + "'",
                    "'" + txtLEmailAddress.Text.Trim() + "'",
                    "'" + float.Parse(txtMonthlyRental.Value.ToString()).ToString() + "'",
                    "'" + txtCLastName.Text.Trim() + "'",
                    "'" + txtCFirstName.Text.Trim() + "'",
                    "'" + txtCMiddleName.Text.Trim() + "'",
                     "'" + txtCTelephoneNumber.Text.Trim() + "'",
                    "'" + txtCCellphoneNumber.Text.Trim() + "'",
                    "'"+txtRemarks.Text.Trim()+"'"
                });

            script.Add(CodeIns.DBU_END);
            //history log to follow
            //Send the file buffer to remote server
            if (hasImage)
            {
                script.Add(CodeIns.RFU_NEW_CORE);
                script.Add(CodeIns.RFU_SET_CONFIG, "ftp://" + Common.CFG.Read("SERVER"), Common.CFG.Read("UID"), Common.CFG.Read("PWD"), 256);
                script.Add(CodeIns.RFU_UPLD, Common.LOCPATH + Const.APPLICATION_EBPLS_IMG, string.Format("{0}/{1}",
                    Common.CFG.Read("DB") + "/tblapplication/", txtReferenceNumber.Text));
                script.Add(CodeIns.RFU_END);
            }
            //script.Add(CodeIns.RFU_END);
            proc.Run(script);
        }
        //used for committing renewal of business permit
        void commitRenewedBusiness()
        {
            //Check if the processor is executing script, if it is, return to the calling code.
            if (proc.State != ProcessorFlag.Ready && proc.State != ProcessorFlag.DataRdy) return;

            string sysDate = txtDateOfApplication.Value.ToString("yyyy-MM-dd");
            script = new CodeScript("RENEWAL");
            script.Add(CodeIns.SYS_RESET);
            script.Add(CodeIns.DBU_NEW_CORE);
            script.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"),
                       Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
            script.Add(CodeIns.DBU_BEGIN_TRANSACTION);
            //application number
            if (isExistingDate)
                script.Add(CodeIns.DBU_UPDATE, "tblGeneratedAppNum", new string[] { "fCurNum" }, new object[] { "fCurNum+1" }, "fID='" + applicationNumberInfo.fID + "' AND fStamp = '" + applicationNumberInfo.fStamp.ToString("yyyy-MM-dd HH:mm:ss") + "'");
            else
                script.Add(CodeIns.DBU_INSERT, "tblGeneratedAppNum", new string[] { "fDate", "fCurNum" }, new object[] { "'" + sysDate + "'", "1" });

            //additional for applicant image
            object imgName = "''";
            if (hasImage) imgName = Sym.PQS(Common.CFG.Read("DB") + "/tblapplication/" + txtReferenceNumber.Text);


            if (txtReferenceNumber.Text.Equals(myReferenceNumber))
            {
                //update business owner profile
                script.Add(CodeIns.DBU_UPDATE, "tblBusinessOwnerProfile",
                       new string[] 
                {   
                    "fCitizenship",     "fCivilStatus",     "fGender",          "fHouseNumber",     "fBuildingName",
                    "fUnitNumber",      "fStreet",          "fBarangay",        "fSubdivision",     "fMunicipality",
                    "fProvince",        "fContactNumber",   "fEmailAddress",    "fProfileType",     "fImgName"
                },
                       new object[] 
                {   

                    "'" + txtCitizenship.Text.Trim() + "'",     "'" + cmbCivilStatus.Text + "'",
                    "'" + cmbGender.Text + "'",                 "'" + txtOHouseNumber.Text.Trim() + "'",
                    "'" + txtOBuildingName.Text.Trim() + "'",   "'" + txtOUnitNumber.Text.Trim() + "'",
                    "'" + txtOSitio.Text.Trim() + "'",          "'" + txtOBarangay.Text.Trim() + "'",
                    "'" + txtOSubdivision.Text.Trim() + "'",    "'" + txtOMunicipality.Text.Trim() + "'",
                    "'" + txtOProvince.Text.Trim() + "'",       "'" + txtOContactNumber.Text.Trim() + "'",
                    "'" + txtOEmailAddress.Text.Trim() + "'",   "'" + businessType + "'", imgName
                }, "fID='" + businessOwner.fID + "' AND fStamp ='" + businessOwner.fStamp.ToString("yyyy-MM-dd HH:mm:ss") + "'");
            }
            else if (txtReferenceNumber.Text.Equals(advanceRefNum.ToString("0000000")))
            {
                //reference number
                script.Add(CodeIns.DBU_UPDATE, "tblGeneratedRefNum", new string[] { "fCurNum" }, new object[] { "fCurNum+1" }, "fID='" + advanceReferenceInfo.fID + "' AND fStamp = '" + advanceReferenceInfo.fStamp.ToString("yyyy-MM-dd HH:mm:ss") + "'");

                //insert new business owner profile 
                script.Add(CodeIns.DBU_INSERT, "tblBusinessOwnerProfile",
                        new string[] 
                    {   "fRegistrationDate",    "fReferenceNumber",     "fCompanyName",     "fLastName",        "fFirstName",       "fMiddleName",
                        "fCitizenship",         "fCivilStatus",         "fGender",          "fHouseNumber",     "fBuildingName",    "fUnitNumber",
                        "fStreet",              "fBarangay",            "fSubdivision",     "fMunicipality",    "fProvince",        "fContactNumber",
                        "fEmailAddress",        "fProfileType",         "fImgName"
                    },
                        new object[] 
                    {   "'" + txtDateOfApplication.Value.ToString("yyyy-MM-dd")+ "'",   
                        "'" + txtReferenceNumber.Text.Trim() + "'",     "'" + txtCompanyName.Text.Trim() + "'",     "'" + txtLastName.Text.Trim() + "'",
                        "'" + txtFirstName.Text.Trim() + "'",           "'" + txtMiddleName.Text.Trim() + "'",      "'" + txtCitizenship.Text.Trim() + "'",                     
                        "'" + cmbCivilStatus.Text + "'",                "'" + cmbGender.Text + "'",                 "'" + txtOHouseNumber.Text.Trim() + "'",
                        "'" + txtOBuildingName.Text.Trim() + "'",       "'" + txtOUnitNumber.Text.Trim() + "'",     "'" + txtOSitio.Text.Trim() + "'",
                        "'" + txtOBarangay.Text.Trim() + "'",           "'" + txtOSubdivision.Text.Trim() + "'",    "'" + txtOMunicipality.Text.Trim() + "'",
                        "'" + txtOProvince.Text.Trim() + "'",           "'" + txtOContactNumber.Text.Trim() + "'",  "'" + txtOEmailAddress.Text.Trim() + "'",
                        "'" + businessType + "'",                       imgName
                    });

                
            }
            else
            {
                //update business owner profile
                script.Add(CodeIns.DBU_UPDATE, "tblBusinessOwnerProfile",
                       new string[] 
                {   
                    "fCitizenship",     "fCivilStatus",     "fGender",          "fHouseNumber",     "fBuildingName",
                    "fUnitNumber",      "fStreet",          "fBarangay",        "fSubdivision",     "fMunicipality",
                    "fProvince",        "fContactNumber",   "fEmailAddress",    "fProfileType",     "fImgName"
                },
                       new object[] 
                {   

                    "'" + txtCitizenship.Text.Trim() + "'",     "'" + cmbCivilStatus.Text + "'",
                    "'" + cmbGender.Text + "'",                 "'" + txtOHouseNumber.Text.Trim() + "'",
                    "'" + txtOBuildingName.Text.Trim() + "'",   "'" + txtOUnitNumber.Text.Trim() + "'",
                    "'" + txtOSitio.Text.Trim() + "'",          "'" + txtOBarangay.Text.Trim() + "'",
                    "'" + txtOSubdivision.Text.Trim() + "'",    "'" + txtOMunicipality.Text.Trim() + "'",
                    "'" + txtOProvince.Text.Trim() + "'",       "'" + txtOContactNumber.Text.Trim() + "'",
                    "'" + txtOEmailAddress.Text.Trim() + "'",   "'" + businessType + "'", imgName
                }, "fID='" + advanceBusinessOwner.fID + "' AND fStamp ='" + advanceBusinessOwner.fStamp.ToString("yyyy-MM-dd HH:mm:ss") + "'");
            }

            //update business profile
            script.Add(CodeIns.DBU_UPDATE, "tblbusinessprofile",
               new string[]
                {
                    "fHouseNumber",
                    "fBuildingName",
                    "fUnitNumber",
                    "fStreet",
                    "fBarangay",
                    "fSubdivision",
                    "fMunicipality",
                    "fProvince",
                    "fContactNumber",
                    "fEmailAddress",
                    "fPropertyIndexNumber",
                    "fBusinessArea",
                    "fNumberOfEmployeesInEstablishment",
                    "fNumberOfEmployeesInTublay",
                    "fLessorLastName",
                    "fLessorFirstName",
                    "fLessorMiddleName",
                    "fLessorAddress",
                    "fLessorContactNumber",
                    "fLessorEmailAddress",
                    "fMonthlyRental",
                    "fContactLastName",
                    "fContactFirstName",
                    "fContactMiddleName",
                    "fContactTelephoneNumber",
                    "fContactCellphoneNumber",
                },
               new object[]
                {
                    "'" + txtBHouseNumber.Text.Trim() + "'",
                    "'" + txtBBuildingName.Text.Trim() + "'",
                    "'" + txtBUnitNumber.Text.Trim() + "'",
                    "'" + txtBSitio.Text.Trim() + "'",
                    "'" + txtBBarangay.Text.Trim() + "'",
                    "'" + txtBSubdivision.Text.Trim() + "'",
                    "'" + txtBMunicipality.Text.Trim() + "'",
                    "'" + txtBProvince.Text.Trim() + "'",
                    "'" + txtBContactNumber.Text.Trim() + "'",
                    "'" + txtBEmailAddress.Text.Trim() + "'",
                    "'" + txtPIN.Text.Trim() + "'",
                    "'" + txtBusinessArea.Text.Trim() + "'",
                    "'" + txtNEmployeesEstablishment.Value.ToString() + "'",
                    "'" + txtNEmployeesTublay.Value.ToString() + "'",
                    "'" + txtLLastName.Text.Trim() + "'",
                    "'" + txtLFirstName.Text.Trim() + "'",
                    "'" + txtLMiddleName.Text + "'",
                    "'" + txtLAddress.Text.Trim() + "'",
                    "'" + txtLContactNumber.Text.Trim() + "'",
                    "'" + txtLEmailAddress.Text.Trim() + "'",
                    "'" + float.Parse(txtMonthlyRental.Value.ToString()).ToString() + "'",
                    "'" + txtCLastName.Text.Trim() + "'",
                    "'" + txtCFirstName.Text.Trim() + "'",
                    "'" + txtCMiddleName.Text.Trim() + "'",
                     "'" + txtCTelephoneNumber.Text.Trim() + "'",
                    "'" + txtCCellphoneNumber.Text.Trim() + "'"
                }, "fID='" + businessProfile.fID + "' AND fStamp ='" + businessProfile.fStamp.ToString("yyyy-MM-dd HH:mm:ss") + "'");

            //update old business information
            //script.Add(CodeIns.DBU_UPDATE, "tblBusinessInformation", new string[] { "fBusinessStatus"}, new object[] { "'RENEWED'" }, "fID='" + businessInformation.fID + "' AND fStamp ='"+businessInformation.fStamp.ToString("yyyy-MM-dd HH:mm:ss")+"'");

            //insert new business information
            script.Add(CodeIns.DBU_INSERT, "tblbusinessinformation",
                new string[]
                {
                    "fDateApplied",
                    "fBusinessReferenceNumber",
                    "fReferenceNumber",
                    "fApplicationNumber",
                    "fBusinessType",
                    "fKindOfOrganization",
                    "fBusinessStatus",
                    "fBusinessName",
                    "fKindOfBusiness",
                    "fTradeName",
                    "fHouseNumber",
                    "fBuildingName",
                    "fUnitNumber",
                    "fStreet",
                    "fBarangay",
                    "fSubdivision",
                    "fMunicipality",
                    "fProvince",
                    "fContactNumber",
                    "fEmailAddress",
                    "fPropertyIndexNumber",
                    "fBusinessArea",
                    "fNumberOfEmployeesInEstablishment",
                    "fNumberOfEmployeesInTublay",
                    "fLessorLastName",
                    "fLessorFirstName",
                    "fLessorMiddleName",
                    "fLessorAddress",
                    "fLessorContactNumber",
                    "fLessorEmailAddress",
                    "fMonthlyRental",
                    "fContactLastName",
                    "fContactFirstName",
                    "fContactMiddleName",
                    "fContactTelephoneNumber",
                    "fContactCellphoneNumber",
                    "fRemarks"
                },
                new object[]
                {
                    "'" + txtDateOfApplication.Value.ToString("yyyy-MM-dd")+ "'",
                    "'" + myBusinessReferenceNumber + "'",
                    "'" + txtReferenceNumber.Text.Trim() + "'",
                    "'" + txtApplicationNumber.Text.Trim() + "'",
                    "'" + businessType + "'",
                    "'" + txtKindOfOrganization.Text.Trim() + "'",
                    "'PENDING'",
                    "'" + txtBusinessName.Text.Trim() + "'",
                    "'" + txtKindOfBusiness.Text.Trim() + "'",
                    "'" + txtTradeNameFranchise.Text.Trim() + "'",
                    "'" + txtBHouseNumber.Text.Trim() + "'",
                    "'" + txtBBuildingName.Text.Trim() + "'",
                    "'" + txtBUnitNumber.Text.Trim() + "'",
                    "'" + txtBSitio.Text.Trim() + "'",
                    "'" + txtBBarangay.Text.Trim() + "'",
                    "'" + txtBSubdivision.Text.Trim() + "'",
                    "'" + txtBMunicipality.Text.Trim() + "'",
                    "'" + txtBProvince.Text.Trim() + "'",
                    "'" + txtBContactNumber.Text.Trim() + "'",
                    "'" + txtBEmailAddress.Text.Trim() + "'",
                    "'" + txtPIN.Text.Trim() + "'",
                    "'" + txtBusinessArea.Text.Trim() + "'",
                    "'" + txtNEmployeesEstablishment.Value.ToString() + "'",
                    "'" + txtNEmployeesTublay.Value.ToString() + "'",
                    "'" + txtLLastName.Text.Trim() + "'",
                    "'" + txtLFirstName.Text.Trim() + "'",
                    "'" + txtLMiddleName.Text + "'",
                    "'" + txtLAddress.Text.Trim() + "'",
                    "'" + txtLContactNumber.Text.Trim() + "'",
                    "'" + txtLEmailAddress.Text.Trim() + "'",
                    "'" + float.Parse(txtMonthlyRental.Value.ToString()).ToString() + "'",
                    "'" + txtCLastName.Text.Trim() + "'",
                    "'" + txtCFirstName.Text.Trim() + "'",
                    "'" + txtCMiddleName.Text.Trim() + "'",
                     "'" + txtCTelephoneNumber.Text.Trim() + "'",
                    "'" + txtCCellphoneNumber.Text.Trim() + "'",
                    "'" + txtRemarks.Text.Trim()+"'"
                });
            script.Add(CodeIns.DBU_END);
            //Send the file buffer to remote server
            if (hasImage)
            {
                script.Add(CodeIns.RFU_NEW_CORE);
                script.Add(CodeIns.RFU_SET_CONFIG, "ftp://" + Common.CFG.Read("SERVER"), Common.CFG.Read("UID"), Common.CFG.Read("PWD"), 256);
                script.Add(CodeIns.RFU_UPLD, Common.LOCPATH + Const.APPLICATION_EBPLS_IMG, string.Format("{0}/{1}",
                    Common.CFG.Read("DB") + "/tblapplication/", txtReferenceNumber.Text));
                script.Add(CodeIns.RFU_END);
            }
            //script.Add(CodeIns.RFU_END);
            proc.Run(script);
        }
        //used for committing transfer of business location
        void commitTransferLocation()
        {
            //Check if the processor is executing script, if it is, return to the calling code.
            if (proc.State != ProcessorFlag.Ready && proc.State != ProcessorFlag.DataRdy) return;
            string sysDate = txtDateOfApplication.Value.ToString("yyyy-MM-dd");
            script = new CodeScript("TRANSFER LOCATION");
            script.Add(CodeIns.SYS_RESET);
            script.Add(CodeIns.DBU_NEW_CORE);
            script.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"),
                       Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
            script.Add(CodeIns.DBU_BEGIN_TRANSACTION);

            //application number
            if (isExistingDate)
                script.Add(CodeIns.DBU_UPDATE, "tblGeneratedAppNum", new string[] { "fCurNum" }, new object[] { "fCurNum+1" }, "fID='" + applicationNumberInfo.fID + "' AND fStamp = '" + applicationNumberInfo.fStamp.ToString("yyyy-MM-dd HH:mm:ss") + "'");
            else
                script.Add(CodeIns.DBU_INSERT, "tblGeneratedAppNum", new string[] { "fDate", "fCurNum" }, new object[] { "'" + sysDate + "'", "1" });

            //additional for applicant image
            object imgName = "''";
            if (hasImage) imgName = Sym.PQS(Common.CFG.Read("DB") + "/tblapplication/" + txtReferenceNumber.Text);

            //update business owner profile
            script.Add(CodeIns.DBU_UPDATE, "tblBusinessOwnerProfile",
                   new string[] 
                {   
                    "fHouseNumber",
                    "fBuildingName",
                    "fUnitNumber",
                    "fStreet",
                    "fBarangay",
                    "fSubdivision",
                    "fMunicipality",
                    "fProvince",
                    "fContactNumber",   
                    "fEmailAddress" 
                },
                   new object[] 
                {   
                    "'" + txtOHouseNumber.Text.Trim() + "'",
                    "'" + txtOBuildingName.Text.Trim() + "'",
                    "'" + txtOUnitNumber.Text.Trim() + "'",
                    "'" + txtOSitio.Text.Trim() + "'",
                    "'" + txtOBarangay.Text.Trim() + "'",
                    "'" + txtOSubdivision.Text.Trim() + "'",
                    "'" + txtOMunicipality.Text.Trim() + "'",
                    "'" + txtOProvince.Text.Trim() + "'",
                    "'" + txtOContactNumber.Text.Trim() + "'",
                    "'" + txtOEmailAddress.Text.Trim() + "'"
                    
                }, "fID='" + businessOwner.fID + "' AND fStamp ='" + businessOwner.fStamp.ToString("yyyy-MM-dd HH:mm:ss") + "'");

            //update business profile
            script.Add(CodeIns.DBU_UPDATE, "tblbusinessprofile",
               new string[]
                {
                         "fHouseNumber",     "   fBuildingName",    "fUnitNumber",
                    "fStreet",          "fBarangay",                "fSubdivision",         "fMunicipality",    "fProvince",
                    "fContactNumber",   "fEmailAddress",            "fPropertyIndexNumber", "fBusinessArea",    "fNumberOfEmployeesInEstablishment",
                    "fNumberOfEmployeesInTublay",
                    "fLessorLastName",  "fLessorFirstName",         "fLessorMiddleName",    "fLessorAddress",   "fLessorContactNumber",
                    "fLessorEmailAddress",
                    "fMonthlyRental",   "fContactLastName",         "fContactFirstName",    "fContactMiddleName",
                    "fContactTelephoneNumber",
                    "fContactCellphoneNumber"
                },
               new object[]
                {
                  
                    "'" + txtBHouseNumber.Text.Trim() + "'",
                    "'" + txtBBuildingName.Text.Trim() + "'",
                    "'" + txtBUnitNumber.Text.Trim() + "'",
                    "'" + txtBSitio.Text.Trim() + "'",
                    "'" + txtBBarangay.Text.Trim() + "'",
                    "'" + txtBSubdivision.Text.Trim() + "'",
                    "'" + txtBMunicipality.Text.Trim() + "'",
                    "'" + txtBProvince.Text.Trim() + "'",
                    "'" + txtBContactNumber.Text.Trim() + "'",
                    "'" + txtBEmailAddress.Text.Trim() + "'",
                    "'" + txtPIN.Text.Trim() + "'",
                    "'" + txtBusinessArea.Text.Trim() + "'",
                    "'" + txtNEmployeesEstablishment.Value.ToString() + "'",
                    "'" + txtNEmployeesTublay.Value.ToString() + "'",
                    "'" + txtLLastName.Text.Trim() + "'",
                    "'" + txtLFirstName.Text.Trim() + "'",
                    "'" + txtLMiddleName.Text + "'",
                    "'" + txtLAddress.Text.Trim() + "'",
                    "'" + txtLContactNumber.Text.Trim() + "'",
                    "'" + txtLEmailAddress.Text.Trim() + "'",
                    "'" + float.Parse(txtMonthlyRental.Value.ToString()).ToString() + "'",
                    "'" + txtCLastName.Text.Trim() + "'",
                    "'" + txtCFirstName.Text.Trim() + "'",
                    "'" + txtCMiddleName.Text.Trim() + "'",
                     "'" + txtCTelephoneNumber.Text.Trim() + "'",
                    "'" + txtCCellphoneNumber.Text.Trim() + "'"
                }, "fID='" + businessProfile.fID + "' AND fStamp ='" + businessProfile.fStamp.ToString("yyyy-MM-dd HH:mm:ss") + "'");

            //update old business information
            //script.Add(CodeIns.DBU_UPDATE, "tblBusinessInformation", new string[] { "fBusinessStatus" }, new object[] { "'TRANSFER LOCATION'" }, "fID='" + businessInformation.fID + "' AND fStamp ='" + businessInformation.fStamp.ToString("yyyy-MM-dd HH:mm:ss") + "'");

            //insert new business information
            script.Add(CodeIns.DBU_INSERT, "tblbusinessinformation",
                new string[]
                {
                    "fDateApplied",
                    "fBusinessReferenceNumber",
                    "fReferenceNumber",
                    "fApplicationNumber",
                    "fBusinessType",
                    "fKindOfOrganization",
                    "fBusinessStatus",
                    "fBusinessName",
                    "fKindOfBusiness",
                    "fTradeName",
                    "fHouseNumber",
                    "fBuildingName",
                    "fUnitNumber",
                    "fStreet",
                    "fBarangay",
                    "fSubdivision",
                    "fMunicipality",
                    "fProvince",
                    "fContactNumber",
                    "fEmailAddress",
                    "fPropertyIndexNumber",
                    "fBusinessArea",
                    "fNumberOfEmployeesInEstablishment",
                    "fNumberOfEmployeesInTublay",
                    "fLessorLastName",
                    "fLessorFirstName",
                    "fLessorMiddleName",
                    "fLessorAddress",
                    "fLessorContactNumber",
                    "fLessorEmailAddress",
                    "fMonthlyRental",
                    "fContactLastName",
                    "fContactFirstName",
                    "fContactMiddleName",
                    "fContactTelephoneNumber",
                    "fContactCellphoneNumber",
                    "fRemarks"
                },
                new object[]
                {
                    "'" + txtDateOfApplication.Value.ToString("yyyy-MM-dd")+ "'",
                    "'" + myBusinessReferenceNumber + "'",
                    "'" + txtReferenceNumber.Text.Trim() + "'",
                    "'" + txtApplicationNumber.Text.Trim() + "'",
                    "'" + businessType + "'",
                    "'" + txtKindOfOrganization.Text.Trim() + "'",
                    "'PENDING'",
                    "'" + txtBusinessName.Text.Trim() + "'",
                    "'" + txtKindOfBusiness.Text.Trim() + "'",
                    "'" + txtTradeNameFranchise.Text.Trim() + "'",
                    "'" + txtBHouseNumber.Text.Trim() + "'",
                    "'" + txtBBuildingName.Text.Trim() + "'",
                    "'" + txtBUnitNumber.Text.Trim() + "'",
                    "'" + txtBSitio.Text.Trim() + "'",
                    "'" + txtBBarangay.Text.Trim() + "'",
                    "'" + txtBSubdivision.Text.Trim() + "'",
                    "'" + txtBMunicipality.Text.Trim() + "'",
                    "'" + txtBProvince.Text.Trim() + "'",
                    "'" + txtBContactNumber.Text.Trim() + "'",
                    "'" + txtBEmailAddress.Text.Trim() + "'",
                    "'" + txtPIN.Text.Trim() + "'",
                    "'" + txtBusinessArea.Text.Trim() + "'",
                    "'" + txtNEmployeesEstablishment.Value.ToString() + "'",
                    "'" + txtNEmployeesTublay.Value.ToString() + "'",
                    "'" + txtLLastName.Text.Trim() + "'",
                    "'" + txtLFirstName.Text.Trim() + "'",
                    "'" + txtLMiddleName.Text + "'",
                    "'" + txtLAddress.Text.Trim() + "'",
                    "'" + txtLContactNumber.Text.Trim() + "'",
                    "'" + txtLEmailAddress.Text.Trim() + "'",
                    "'" + float.Parse(txtMonthlyRental.Value.ToString()).ToString() + "'",
                    "'" + txtCLastName.Text.Trim() + "'",
                    "'" + txtCFirstName.Text.Trim() + "'",
                    "'" + txtCMiddleName.Text.Trim() + "'",
                     "'" + txtCTelephoneNumber.Text.Trim() + "'",
                    "'" + txtCCellphoneNumber.Text.Trim() + "'",
                    "'" + txtRemarks.Text.Trim()+ "'"
                });
            script.Add(CodeIns.DBU_END);
            //Send the file buffer to remote server
            if (hasImage)
            {
                script.Add(CodeIns.RFU_NEW_CORE);
                script.Add(CodeIns.RFU_SET_CONFIG, "ftp://" + Common.CFG.Read("SERVER"), Common.CFG.Read("UID"), Common.CFG.Read("PWD"), 256);
                script.Add(CodeIns.RFU_UPLD, Common.LOCPATH + Const.APPLICATION_EBPLS_IMG, string.Format("{0}/{1}",
                    Common.CFG.Read("DB") + "/tblapplication/", txtReferenceNumber.Text));
                script.Add(CodeIns.RFU_END);
            }
            //script.Add(CodeIns.RFU_END);
            proc.Run(script);
        }
        //used for committing transfer of business location
        void commitTransferOwnership()
        {
            //Check if the processor is executing script, if it is, return to the calling code.
            if (proc.State != ProcessorFlag.Ready && proc.State != ProcessorFlag.DataRdy) return;
            string sysDate = txtDateOfApplication.Value.ToString("yyyy-MM-dd");
            script = new CodeScript("TRANSFER OWNERSHIP");
            script.Add(CodeIns.SYS_RESET);
            script.Add(CodeIns.DBU_NEW_CORE);
            script.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"),
                       Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
            script.Add(CodeIns.DBU_BEGIN_TRANSACTION);

            if (txtReferenceNumber.Text.Equals(myReferenceNumber))
                return;
            //application number
            if (isExistingDate)
                script.Add(CodeIns.DBU_UPDATE, "tblGeneratedAppNum", new string[] { "fCurNum" }, new object[] { "fCurNum+1" }, "fID='" + applicationNumberInfo.fID + "' AND fStamp = '" + applicationNumberInfo.fStamp.ToString("yyyy-MM-dd HH:mm:ss") + "'");
            else
                script.Add(CodeIns.DBU_INSERT, "tblGeneratedAppNum", new string[] { "fDate", "fCurNum" }, new object[] { "'" + sysDate + "'", "1" });

            //additional for applicant image
            object imgName = "''";
            if (hasImage) imgName = Sym.PQS(Common.CFG.Read("DB") + "/tblapplication/" + txtReferenceNumber.Text);

            if (txtReferenceNumber.Text.Equals(myReferenceNumber))
            {
                //update business owner profile
                script.Add(CodeIns.DBU_UPDATE, "tblBusinessOwnerProfile",
                       new string[] 
                {   
                    "fCitizenship",     "fCivilStatus",     "fGender",          "fHouseNumber",     "fBuildingName",
                    "fUnitNumber",      "fStreet",          "fBarangay",        "fSubdivision",     "fMunicipality",
                    "fProvince",        "fContactNumber",   "fEmailAddress",    "fProfileType",     "fImgName"
                },
                       new object[] 
                {   

                    "'" + txtCitizenship.Text.Trim() + "'",     "'" + cmbCivilStatus.Text + "'",
                    "'" + cmbGender.Text + "'",                 "'" + txtOHouseNumber.Text.Trim() + "'",
                    "'" + txtOBuildingName.Text.Trim() + "'",   "'" + txtOUnitNumber.Text.Trim() + "'",
                    "'" + txtOSitio.Text.Trim() + "'",          "'" + txtOBarangay.Text.Trim() + "'",
                    "'" + txtOSubdivision.Text.Trim() + "'",    "'" + txtOMunicipality.Text.Trim() + "'",
                    "'" + txtOProvince.Text.Trim() + "'",       "'" + txtOContactNumber.Text.Trim() + "'",
                    "'" + txtOEmailAddress.Text.Trim() + "'",   "'" + businessType + "'", imgName
                }, "fID='" + businessOwner.fID + "' AND fStamp ='" + businessOwner.fStamp.ToString("yyyy-MM-dd HH:mm:ss") + "'");
            }
            else if (txtReferenceNumber.Text.Equals(advanceRefNum.ToString("0000000")))
            {
                //reference number
                script.Add(CodeIns.DBU_UPDATE, "tblGeneratedRefNum", new string[] { "fCurNum" }, new object[] { "fCurNum+1" }, "fID='" + advanceReferenceInfo.fID + "' AND fStamp = '" + advanceReferenceInfo.fStamp.ToString("yyyy-MM-dd HH:mm:ss") + "'");

                //insert new business owner profile 
                script.Add(CodeIns.DBU_INSERT, "tblBusinessOwnerProfile",
                        new string[] 
                    {   "fRegistrationDate",    "fReferenceNumber",     "fCompanyName",     "fLastName",        "fFirstName",       "fMiddleName",
                        "fCitizenship",         "fCivilStatus",         "fGender",          "fHouseNumber",     "fBuildingName",    "fUnitNumber",
                        "fStreet",              "fBarangay",            "fSubdivision",     "fMunicipality",    "fProvince",        "fContactNumber",
                        "fEmailAddress",        "fProfileType",         "fImgName"
                    },
                        new object[] 
                    {   "'" + txtDateOfApplication.Value.ToString("yyyy-MM-dd")+ "'",   
                        "'" + txtReferenceNumber.Text.Trim() + "'",     "'" + txtCompanyName.Text.Trim() + "'",     "'" + txtLastName.Text.Trim() + "'",
                        "'" + txtFirstName.Text.Trim() + "'",           "'" + txtMiddleName.Text.Trim() + "'",      "'" + txtCitizenship.Text.Trim() + "'",                     
                        "'" + cmbCivilStatus.Text + "'",                "'" + cmbGender.Text + "'",                 "'" + txtOHouseNumber.Text.Trim() + "'",
                        "'" + txtOBuildingName.Text.Trim() + "'",       "'" + txtOUnitNumber.Text.Trim() + "'",     "'" + txtOSitio.Text.Trim() + "'",
                        "'" + txtOBarangay.Text.Trim() + "'",           "'" + txtOSubdivision.Text.Trim() + "'",    "'" + txtOMunicipality.Text.Trim() + "'",
                        "'" + txtOProvince.Text.Trim() + "'",           "'" + txtOContactNumber.Text.Trim() + "'",  "'" + txtOEmailAddress.Text.Trim() + "'",
                        "'" + businessType + "'",                       imgName
                    });
                
            }
            else
            {
                //update business owner profile
                script.Add(CodeIns.DBU_UPDATE, "tblBusinessOwnerProfile",
                       new string[] 
                {   
                    "fCitizenship",     "fCivilStatus",     "fGender",          "fHouseNumber",     "fBuildingName",
                    "fUnitNumber",      "fStreet",          "fBarangay",        "fSubdivision",     "fMunicipality",
                    "fProvince",        "fContactNumber",   "fEmailAddress",    "fProfileType",     "fImgName"
                },
                       new object[] 
                {   

                    "'" + txtCitizenship.Text.Trim() + "'",     "'" + cmbCivilStatus.Text + "'",
                    "'" + cmbGender.Text + "'",                 "'" + txtOHouseNumber.Text.Trim() + "'",
                    "'" + txtOBuildingName.Text.Trim() + "'",   "'" + txtOUnitNumber.Text.Trim() + "'",
                    "'" + txtOSitio.Text.Trim() + "'",          "'" + txtOBarangay.Text.Trim() + "'",
                    "'" + txtOSubdivision.Text.Trim() + "'",    "'" + txtOMunicipality.Text.Trim() + "'",
                    "'" + txtOProvince.Text.Trim() + "'",       "'" + txtOContactNumber.Text.Trim() + "'",
                    "'" + txtOEmailAddress.Text.Trim() + "'",   "'" + businessType + "'", imgName
                }, "fID='" + advanceBusinessOwner.fID + "' AND fStamp ='" + advanceBusinessOwner.fStamp.ToString("yyyy-MM-dd HH:mm:ss") + "'");
            }
            //update business profile
            script.Add(CodeIns.DBU_UPDATE, "tblbusinessprofile",
               new string[]
                {
                    "fBusinessType",    "fKindOfOrganization",      "fHouseNumber",     "   fBuildingName",    "fUnitNumber",
                    "fStreet",          "fBarangay",                "fSubdivision",         "fMunicipality",    "fProvince",
                    "fContactNumber",   "fEmailAddress",            "fPropertyIndexNumber", "fBusinessArea",    "fNumberOfEmployeesInEstablishment",
                    "fNumberOfEmployeesInTublay",
                    "fLessorLastName",  "fLessorFirstName",         "fLessorMiddleName",    "fLessorAddress",   "fLessorContactNumber",
                    "fLessorEmailAddress",
                    "fMonthlyRental",   "fContactLastName",         "fContactFirstName",    "fContactMiddleName",
                    "fContactTelephoneNumber",
                    "fContactCellphoneNumber",          "fReferenceNumber"
                },
               new object[]
                {
                    "'" + businessType + "'",
                    "'" + txtKindOfOrganization.Text.Trim() + "'",
                    "'" + txtBHouseNumber.Text.Trim() + "'",
                    "'" + txtBBuildingName.Text.Trim() + "'",
                    "'" + txtBUnitNumber.Text.Trim() + "'",
                    "'" + txtBSitio.Text.Trim() + "'",
                    "'" + txtBBarangay.Text.Trim() + "'",
                    "'" + txtBSubdivision.Text.Trim() + "'",
                    "'" + txtBMunicipality.Text.Trim() + "'",
                    "'" + txtBProvince.Text.Trim() + "'",
                    "'" + txtBContactNumber.Text.Trim() + "'",
                    "'" + txtBEmailAddress.Text.Trim() + "'",
                    "'" + txtPIN.Text.Trim() + "'",
                    "'" + txtBusinessArea.Text.Trim() + "'",
                    "'" + txtNEmployeesEstablishment.Value.ToString() + "'",
                    "'" + txtNEmployeesTublay.Value.ToString() + "'",
                    "'" + txtLLastName.Text.Trim() + "'",
                    "'" + txtLFirstName.Text.Trim() + "'",
                    "'" + txtLMiddleName.Text + "'",
                    "'" + txtLAddress.Text.Trim() + "'",
                    "'" + txtLContactNumber.Text.Trim() + "'",
                    "'" + txtLEmailAddress.Text.Trim() + "'",
                    "'" + float.Parse(txtMonthlyRental.Value.ToString()).ToString() + "'",
                    "'" + txtCLastName.Text.Trim() + "'",
                    "'" + txtCFirstName.Text.Trim() + "'",
                    "'" + txtCMiddleName.Text.Trim() + "'",
                     "'" + txtCTelephoneNumber.Text.Trim() + "'",
                    "'" + txtCCellphoneNumber.Text.Trim() + "'", 
                    "'" +txtReferenceNumber.Text.Trim()+ "'"
                }, "fID='" + businessProfile.fID + "' AND fStamp ='" + businessProfile.fStamp.ToString("yyyy-MM-dd HH:mm:ss") + "'");

            //update old business information
            //script.Add(CodeIns.DBU_UPDATE, "tblBusinessInformation", new string[] { "fBusinessStatus" }, new object[] { "'TRANSFER OWNERSHIP'" }, "fID='" + businessInformation.fID + "' AND fStamp ='" + businessInformation.fStamp.ToString("yyyy-MM-dd HH:mm:ss") + "'");

            //insert new business information
            script.Add(CodeIns.DBU_INSERT, "tblbusinessinformation",
                new string[]
                {
                    "fDateApplied",
                    "fBusinessReferenceNumber",
                    "fReferenceNumber",
                    "fApplicationNumber",
                    "fBusinessType",
                    "fKindOfOrganization",
                    "fBusinessStatus",
                    "fBusinessName",
                    "fKindOfBusiness",
                    "fTradeName",
                    "fHouseNumber",
                    "fBuildingName",
                    "fUnitNumber",
                    "fStreet",
                    "fBarangay",
                    "fSubdivision",
                    "fMunicipality",
                    "fProvince",
                    "fContactNumber",
                    "fEmailAddress",
                    "fPropertyIndexNumber",
                    "fBusinessArea",
                    "fNumberOfEmployeesInEstablishment",
                    "fNumberOfEmployeesInTublay",
                    "fLessorLastName",
                    "fLessorFirstName",
                    "fLessorMiddleName",
                    "fLessorAddress",
                    "fLessorContactNumber",
                    "fLessorEmailAddress",
                    "fMonthlyRental",
                    "fContactLastName",
                    "fContactFirstName",
                    "fContactMiddleName",
                    "fContactTelephoneNumber",
                    "fContactCellphoneNumber",
                    "fRemarks"
                },
                new object[]
                {
                    "'" + txtDateOfApplication.Value.ToString("yyyy-MM-dd")+ "'",
                    "'" + myBusinessReferenceNumber + "'",
                    "'" + txtReferenceNumber.Text.Trim() + "'",
                    "'" + txtApplicationNumber.Text.Trim() + "'",
                    "'" + businessType + "'",
                    "'" + txtKindOfOrganization.Text.Trim() + "'",
                    "'PENDING'",
                    "'" + txtBusinessName.Text.Trim() + "'",
                    "'" + txtKindOfBusiness.Text.Trim() + "'",
                    "'" + txtTradeNameFranchise.Text.Trim() + "'",
                    "'" + txtBHouseNumber.Text.Trim() + "'",
                    "'" + txtBBuildingName.Text.Trim() + "'",
                    "'" + txtBUnitNumber.Text.Trim() + "'",
                    "'" + txtBSitio.Text.Trim() + "'",
                    "'" + txtBBarangay.Text.Trim() + "'",
                    "'" + txtBSubdivision.Text.Trim() + "'",
                    "'" + txtBMunicipality.Text.Trim() + "'",
                    "'" + txtBProvince.Text.Trim() + "'",
                    "'" + txtBContactNumber.Text.Trim() + "'",
                    "'" + txtBEmailAddress.Text.Trim() + "'",
                    "'" + txtPIN.Text.Trim() + "'",
                    "'" + txtBusinessArea.Text.Trim() + "'",
                    "'" + txtNEmployeesEstablishment.Value.ToString() + "'",
                    "'" + txtNEmployeesTublay.Value.ToString() + "'",
                    "'" + txtLLastName.Text.Trim() + "'",
                    "'" + txtLFirstName.Text.Trim() + "'",
                    "'" + txtLMiddleName.Text + "'",
                    "'" + txtLAddress.Text.Trim() + "'",
                    "'" + txtLContactNumber.Text.Trim() + "'",
                    "'" + txtLEmailAddress.Text.Trim() + "'",
                    "'" + float.Parse(txtMonthlyRental.Value.ToString()).ToString() + "'",
                    "'" + txtCLastName.Text.Trim() + "'",
                    "'" + txtCFirstName.Text.Trim() + "'",
                    "'" + txtCMiddleName.Text.Trim() + "'",
                     "'" + txtCTelephoneNumber.Text.Trim() + "'",
                    "'" + txtCCellphoneNumber.Text.Trim() + "'",
                    "'" + txtRemarks.Text.Trim()+ "'"
                });
            script.Add(CodeIns.DBU_END);
            //Send the file buffer to remote server
            if (hasImage)
            {
                script.Add(CodeIns.RFU_NEW_CORE);
                script.Add(CodeIns.RFU_SET_CONFIG, "ftp://" + Common.CFG.Read("SERVER"), Common.CFG.Read("UID"), Common.CFG.Read("PWD"), 256);
                script.Add(CodeIns.RFU_UPLD, Common.LOCPATH  + Const.APPLICATION_EBPLS_IMG, string.Format("{0}/{1}",
                    Common.CFG.Read("DB") + "/tblapplication/", txtReferenceNumber.Text));
                script.Add(CodeIns.RFU_END);
            }
            //script.Add(CodeIns.RFU_END);
            proc.Run(script);
        }
        //used for checking if there is existing owner profile 
        #endregion

        #region Clearing Methods

        //clear basic fields
        void clearBasicInfo()
        {
            txtLastName.Text = "";
            txtFirstName.Text = "";
            txtMiddleName.Text = "";
            txtCompanyName.Text = "";
            lblErrorMsg.Text = "";
            btnSave.Enabled = true;
        }
        //clear address
        void clearAddress()
        {
            cmbGender.SelectedIndex = 0;
            cmbCivilStatus.SelectedIndex = 0;
            txtCitizenship.Text = "";
            txtOHouseNumber.Text = "";
            txtOBuildingName.Text = "";
            txtOUnitNumber.Text = "";
            txtOSitio.Text = "";
            txtOBarangay.Text = "";
            txtOSubdivision.Text = "";
            txtOMunicipality.Text = "TUBLAY";
            txtOProvince.Text = "BENGUET";
            txtOContactNumber.Text = "";
            txtOEmailAddress.Text = "";
        }
        //methods for radio button
        void executeClearing(bool state)
        {
            grpEnabling(state);
            clearBasicInfo();
            clearAddress();
            txtReferenceNumber.Text = currentRefNum.ToString("0000000");
            isExisitngOwnerProfile = false;
        }
        //clear fields
        void clearTheSame()
        {
            txtBHouseNumber.Text = "";
            txtBBuildingName.Text = "";
            txtBUnitNumber.Text = "";
            txtBSitio.Text = "";
            txtBBarangay.Text = "";
            txtBSubdivision.Text = "";
            txtBMunicipality.Text = "Tublay";
            txtBProvince.Text = "Benguet";
            txtBContactNumber.Text = "";
            txtBEmailAddress.Text = "";
        }
        #endregion


    }
}
